How would I split a string by capital letters?
Example:
local mystring = "MyString"
mystring = string.split(mystring, --[[A capital letter string identifier]])
mystring = table.concat(mystring, " ")
>> "My String"
This would add a space between every capital letter in a string
I could use things like looping through each letter and using if letter = string.upper(letter) but i was hoping there was a simpler way like shown above
I’m not sure using the split method would be very useful.
You can loop through each character and check if string.upper(char) returns char, in which case it is an uppercase letter and you can add a space.
string.upper(char) would return an uppercase character even if the input was a lowercase character. Never mind, you mean check the character against its uppercase variant. That would be fairly long-winded, especially when you can do the following instead.
local function splitTitleCaps(str)
str = str:gsub("(%u)", " %1")
return str:gsub("^%s", "")
end
local res = splitTitleCaps("MyString")
print(res) --My String