How to split string by string length and a separator? - lua

I'm trying to split a string into 2 strings when main string is over 30 chars and separator I wanted to use is a simple space between chars(the last space between words in main string) so it won't cut words. I'm asking you guys for help because I'm not very good with patterns in Lua.

local function split(str, max_line_length)
local lines = {}
local line
str:gsub('(%s*)(%S+)',
function(spc, word)
if not line or #line + #spc + #word > max_line_length then
table.insert(lines, line)
line = word
else
line = line..spc..word
end
end
)
table.insert(lines, line)
return lines
end
local main_string = 'This is very very very very very very long string'
for _, line in ipairs(split(main_string, 20)) do
print(line)
end
-- Output
This is very very
very very very very
long string

If you just want to split the string at the last space between words, try this
s="How to split string by string length and a separator"
a,b=s:match("(.+) (.+)")
print(s)
print(a)
print(b)

Related

how to get numbers from string in LUA?

I have string "0,0,0,-58.43083113,,"
and how could I get all the 4 numbers as double with LUA? Thanks!
I have tried with string.match(). But it didn't work.
local text = "0,0,0,-58.43083113,,"
local numbers = {}
text:gsub("[^,]+", function (str) table.insert(numbers, tonumber(str)+.0) end)
print(table.concat(numbers, ", "))
or
for str in text:gmatch("[^,]+") do
table.insert(numbers, tonumber(str) + .0)
end
Of course this assumes that you only have number representations and commas in your string.

Lua How do I add a space between small and capital letters

I have a function to put the first letter of a string into uppercase.
function firstToUpper(str)
return string.gsub(" "..str, "%W%l", string.upper):sub(2)
end
Now I need a function to add a space between small and big letters in a string like:
HelloWorld ----> Hello World
Do you know any solution for Lua?
str:gsub("(%l)(%u)", "%1 %2") returns a string that comes with a space between any lower upper letter pair in str.
Please read https://www.lua.org/manual/5.4/manual.html#pdf-string.gsub
local function spaceOut(str)
local new = str
repeat
local start,finish = new:find("%l%u")
new = new:gsub("%l%u",new:sub(start,start).." "..new:sub(finish,finish),1)
until new:find("%l%u") == nil
return new
end
print(spaceOut("ThisIsMyMethodForSpacingWordsOut"))

Trying to make function which takes string as input and returns no. of words in whole string

**It takes Input as a string such as this - 'Nice one'
And Output gives - 4,3 (which is no. Of words in sentence or string)
**
function countx(str)
local count = {}
for i = 1, string.len(str) do
s = ''
while (i<=string.len(str) and string.sub(str, i, i) ~= ' ' ) do
s = s .. string.sub(str, i, i)
i = i+1
end
if (string.len(s)>0) then
table.insert(count,string.len(s))
end
end
return table.concat(count, ',')
end
You can find a simple alternative with your new requirements:
function CountWordLength (String)
local Results = { }
local Continue = true
local Position = 1
local SpacePosition
while Continue do
SpacePosition = string.find(String, " ", Position)
if SpacePosition then
Results[#Results + 1] = SpacePosition - Position
Position = SpacePosition + 1
-- if needed to print the string
-- local SubString = String:sub(Position, SpacePosition)
-- print(SubString)
else
Continue = false
end
end
Results[#Results + 1] = #String - Position + 1
return Results
end
Results = CountWordLength('I am a boy')
for Index, Value in ipairs(Results) do
print(Value)
end
Which gives the following results:
1
2
1
3
def countLenWords(s):
s=s.split(" ")
s=map(len,s)
s=map(str,s)
s=list(s)
return s
The above functions returns a list containing number of characters in each word
s=s.split(" ") splits string with delimiter " " (space)
s=map(len,s) maps the words into length of the words in int
s=map(str,s) maps the values into string
s=list(s) converts map object to list
Short version of above function (all in one line)
def countLenWords(s):
return list(map(str,map(len,s.split(" "))))
-- Localise for performance.
local insert = table.insert
local text = 'I am a poor boy straight. I do not need sympathy'
local function word_lengths (text)
local lengths = {}
for word in text:gmatch '[%l%u]+' do
insert (lengths, word:len())
end
return lengths
end
print ('{' .. table.concat (word_lengths (text), ', ') .. '}')
gmatch returns an iterator over matches of a pattern in a string.
[%l%u]+ is a Lua regular expression (see http://lua-users.org/wiki/PatternsTutorial) matching at least one lowercase or uppercase letter:
[] is a character class: a set of characters. It matches anything inside brackets, e.g. [ab] will match both a and b,
%l is any lowercase Latin letter,
%u is any uppercase Latin letter,
+ means one or more repeats.
Therefore, text:gmatch '[%l%u]+' will return an iterator that will produce words, consisting of Latin letters, one by one, until text is over. This iterator is used in generic for (see https://www.lua.org/pil/4.3.5.html); and on any iteration word will contain a full match of the regular expression.

How do I extract numbers from a string?

How would I extract every number from a string and put them in an array?
For example the string:
"\113\115\106\111\117\41\40\105\102\109\109\112\40\42"
You can use string.gmatch like this:
local my_array = {}
local my_string = "\\113\\115\\106\\111\\117\\41\\40\\105\\102\\109\\109\\112\\40\\42"
print(my_string) --note how the string is \ followed by digits
for number in string.gmatch(my_string, "\\(%d+)") do
my_array[#my_array + 1] = tonumber(number)
print(number)
end
This will get you an table with all the numbers from your string.
The \ is escaped in my example to make it equal to the string you stated.
If i misunderstood your question and the numbers you want are from the chars then you need to do
local my_array = {}
local my_string = "\113\115\106\111\117\41\40\105\102\109\109\112\40\42"
print(my_string) --note how the string is letters
for char in string.gmatch(my_string, ".") do
my_array[#my_array + 1] = string.byte(char)
print(char, my_array[#my_array])
end

Get numerical values txt file using delimiters

I have a txt file with the following text:
5;2;8;3;
I need get the numeric values, using ; as delimiter, and put them into an array. How could this be achieved?
The easiest way is to just use string.gmatch to match the numbers:
local example = "5;2;8;3;"
for i in string.gmatch(example, "%d+") do
print(i)
end
Output:
5
2
8
3
A "harder" way with a specific Split function:
function split(str, delimiter)
local result = {}
local regex = string.format("([^%s]+)%s", delimiter, delimiter)
for entry in str:gmatch(regex) do
table.insert(result, entry)
end
return result
end
local split_ex = split(example, ";")
print(unpack(split_ex))
Output:
5 2 8 3
Have a look at a sample program here.

Resources