I need a special Lua pattern that takes all the uppercase letters in a string, and replaces them with a space and the respective lowercase letter;
TestStringOne => test string one
this isA TestString => this is a test string
Can it be done?
Assuming only ASCII is used, this works:
function lowercase(str)
return (str:gsub("%u", function(c) return ' ' .. c:lower() end))
end
print(lowercase("TestStringOne"))
print(lowercase("this isA TestString"))
function my(s)
s = s:gsub('(%S)(%u)', '%1 %2'):lower()
return s
end
print(my('TestStringOne')) -->test string one
print(my('this isA TestString')) -->this is a test string
Related
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"))
#lf_araujo asked in this question:
var dic = new dict of string, string
dic["z"] = "23"
dic["abc"] = "42"
dic["pi"] = "3.141"
for k in sorted_string_collection (dic.keys)
print (#"$k: $(dic[k])")
What is the function of # in print(# ... ) and lines_add(# ...)?
As this is applicable to both Genie and Vala, I thought it would be better suited as a stand-alone question.
The conceptual question is:
How does string interpolation work in Vala and Genie?
There are two options for string interpolation in Vala and Genie:
printf-style functions:
var name = "Jens Mühlenhoff";
var s = string.printf ("My name is %s, 2 + 2 is %d", name, 2 + 2);
This works using varargs, you have to pass multiple arguments with the correct types to the varargs function (in this case string.printf).
string templates:
var name = "Jens Mühlenhoff";
var s = #"My name is $name, 2 + 2 is $(2 + 2)";
This works using "compiler magic".
A template string starts with #" (rather then " which starts a normal string).
Expressions in the template string start with $ and are enclosed with (). The brackets are unneccesary when the expression doesn't contain white space like $name in the above example.
Expressions are evaluated before they are put into the string that results from the string template. For expressions that aren't of type string the compiler tries to call .to_string (), so you don't have to explicitly call it. In the $(2 + 2) example the expression 2 + 2 is evaluated to 4 and then 4.to_string () is called with will result in "4" which can then be put into the string template.
PS: I'm using Vala syntax here, just remove the ;s to convert to Genie.
I checking string for a non-alphanumeric char.
if(str:match("%W")) then
--make str alpha-numeric
end
How to remove all non-alphanumeric chars from string using lua?
Use gsub (as suggested by Egor Skriptunoff):
str = str:gsub('%W','')
just make it like this
you forgot +
if(str:match("%W+")) then --if it contain alpha
number = str:match("%d+")
alpha = str:match("%W+")
end
Hi I've got this function in JavaScript:
function blur(data) {
var trimdata = trim(data);
var dataSplit = trimdata.split(" ");
var lastWord = dataSplit.pop();
var toBlur = dataSplit.join(" ");
}
What this does is it take's a string such as "Hello my name is bob" and will return
toBlur = "Hello my name is" and lastWord = "bob"
Is there a way i can re-write this in Lua?
You could use Lua's pattern matching facilities:
function blur(data) do
return string.match(data, "^(.*)[ ][^ ]*$")
end
How does the pattern work?
^ # start matching at the beginning of the string
( # open a capturing group ... what is matched inside will be returned
.* # as many arbitrary characters as possible
) # end of capturing group
[ ] # a single literal space (you could omit the square brackets, but I think
# they increase readability
[^ ] # match anything BUT literal spaces... as many as possible
$ # marks the end of the input string
So [ ][^ ]*$ has to match the last word and the preceding space. Therefore, (.*) will return everything in front of it.
For a more direct translation of your JavaScript, first note that there is no split function in Lua. There is table.concat though, which works like join. Since you have to do the splitting manually, you'll probably use a pattern again:
function blur(data) do
local words = {}
for m in string.gmatch("[^ ]+") do
words[#words+1] = m
end
words[#words] = nil -- pops the last word
return table.concat(words, " ")
end
gmatch does not give you a table right away, but an iterator over all matches instead. So you add them to your own temporary table, and call concat on that. words[#words+1] = ... is a Lua idiom to append an element to the end of an array.
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)