Help with string modification

Hello developers, I’m trying to make code that needs this:

 TestString location = "hello_bye"
 -- I want to transform "TestString" into:
 -- Hello bye

I want each word to capitalize the first letter and each “_” into a space.
If you can help me with that I would appreciate it :slight_smile:

There is probably an easier way of doing this although

local TestString = "hello_bye_yes"
TestString = TestString:gsub("_", " "):gsub("%S+", function(s)
    return s:gsub("^%l", string.upper, 1)
end)
print(TestString)

works.

I first start off by replacing all underscores with spaces, then going through non-spaces so i get just the words, then replace the first lowercase letter of the first word with its uppercase complement.

1 Like

I think he just wanted the first letter in the string to be capitalized?

local function Format(str)
  local s = str:gsub("_", " "):gsub("^%l", string.upper, 1)
  return s
end
1 Like