^
in the beginning of a pattern is the anchor for "the beginning of the string", i.e,"^%a"
matches a"%a"
(a letter) only if it's in the beginning of the string.The first
string.gsub()
is to make the first letter in the string uppercase, the secondstring.gsub()
is to make the first letter of the other words (a letter that is follows a whitespace) uppercase.For instance, a string
"hello world"
turns into"Hello world"
after the firststring.gsub()
, then turns into"Hello World"
after the secondstring.gsub()
.
Use of ^ and gsub in Lua
-
28-06-2023 - |
質問
function title case(theString)
return (theString:gsub("^%a", string.upper):gsub("%s+%a", string.upper))
end
I have the above mentioned code. I wanted to know the use of
^
operator in the above code. I know that^
is used in sets to exclude/negate the succeeding values in a set but since it's not a set, what is^
meant for ?Why is
gsub
used twice in above code? Both the times, the letters are being converted to upper case! Please explain what's exactly happening in above line of code.
解決
所属していません StackOverflow