Example: "the quick brown fox jumps over the lazy dog " to "The Quick Brown Fox Jumps Over The Lazy Dog"
COMMUNITY QUESTION
How do I capitalize the first letter of every word of a given string value?
1 answer
Split it by space, grab the first letter of every word, upper it and then put everything back together.
local str = "a quick brown fox blabla"
local spl = string.split(str, " ") --split by space
for i,word in pairs(spl) do
local firstLetter = word:sub(1,1) --get the 1st letter of the word
local theRest = word:sub(2) --grt everything except the first letter
spl[i] = firstLetter:upper() .. theRest
end
local result = table.concat(spl, " ") --put it back together
print(result)
A shorter version:
local str = "a quick brown fox blabla"
local spl = string.split(str, " ")
for i,v in pairs(spl) do
spl[i] = v:sub(1,1):upper() .. v:sub(2)
end
local result = table.concat(spl, " ")
This discussion is preserved as it was. New answers, comments, and votes are closed.
