How to change the case and add space in a string

Hello! I would like know how to change the case and add space in a string to make another string.

I would like a space between a lowercase and a uppercase and all uppercase become a lowercase except the first uppercase.

Exemples:

  • HelloWorld = Hello world
  • BrickRed = Brick red
  • GreyMouse = Grey mouse

Could you show me a script please?

You can first separate the words by finding uppercase letters, then make the whole thing lowercase, and at the end make the first letter uppercase.

local str = "HelloWorld"

local str2 = str:gsub("%u", " %1"):lower()

local result = str2:sub(1,2):upper()..str2:sub(3)

print(result)
2 Likes