Reputation: 21
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?
Upvotes: 2
Views: 1417
Reputation: 28950
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
Upvotes: 4
Reputation: 1
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"))
Upvotes: 0