Add Spaces to Camel Case

Hello Developers!

While this isn’t much of a resource, somehow not a single article I have found has listed a correct solution.

I often like to store things in UpperCamelCase/lowerCamelCase for config, yet display them with spaces. I figured I could just copy the function from someone on here quickly because I always forget the gsub codes, but there aren’t any solutions that work on strings of unspecified lengths.

local function addSpaces(str)
	local final, _ = str:gsub("(%l)(%u)", "%1 %2")
	return final
end

print(addSpaces("HelloWorld")) -- Hello World
print(addSpaces("HelloMyFriendMr.ROLO")) -- Hello My Friend Mr.ROLO
How It Works

str:gsub replaces all instances of the first pattern with the second pattern. You will also notice I am using the instance method(obj:method(a,b)), not static(class.method(obj,a,b). I could use the Static gsub method of the string class as follows.

local function addSpaces(str)
	local final, _ = string.gsub(str, "(%l)(%u)", "%1 %2")
	return final
end

You will also notice that I save the returned values as final, _. This is because gsub returns two values, the new string, and how many replacements were made. As for the actual arguments to the gsub method there is the pattern and the replacement. The first argument, the pattern, searches for all instances where a lower case letter precedes a Capital letter. The second argument, the replacement, specifies what should be replaced in the new string. In this case, we want to keep the original two letters, but add a space between them. You can read more about gsub codes here: Strings | Documentation - Roblox Creator Hub. Thank you for reading!

I hope this helps you shorten up your config values, while displaying more visually appealing information to users. Good luck in your development journeys!

9 Likes

Interesting approach!


Why store the final?

1 Like

If you read the “How It Works” segment, gsub returns two values. The first is the new string, and the second is the amount of replacements made. It goes instantly to garbage collection and just stores a string, so it’s not like there’s a lot of memory usage.

1 Like

Thanks for posting this! I didn’t know you could do that with string patterns.

To shorten that code, you can do this:

-- original:
local function addSpaces(str)
	local final, _ = str:gsub("(%l)(%u)", "%1 %2")
	return final
end
-- shortened:
local function addSpaces(str)
	return (str:gsub("(%l)(%u)", "%1 %2"))
end
print(addSpaces("HelloWorld!")) --> Hello World!
print(addSpaces("Hello World!")) --> Hello World!

I don’t understand what you are attempting to accomplish here. This is community resources, and I wanted to explain how gsub works. It returns two values, the new string and a number of changes made. It’s an incredibly short code segment, and the function is terminated almost instantly, cleaning up any extra memory usage.

2 Likes

I don’t know how this works an video will be better?
Or image

hes trying to teach u good scripting habits, making a variable instead of returning it instantly just consumes time for no reason when you could just return it, its like saying

function IsNumberEven(Num)
local IsEven = Num % 2 == 0
return IsEven
end

when you can just do this

function IsNumberEven(Num)
return Num % 2 == 0
end

Except in this case, gsub actually returns two arguments. Merely returning all values returned by it would give you a string and a number. I explained this in the How It Works segment, that is why I include the _. When you have a value you don’t need to use, it is common practice to “discard” it with an _.

1 Like

i think you used chatgpt for this script im not gonna lie if u didnt understand what i said u have no basic knowledge of scripting

If you read my message, returning all that gsub returns gives both the new string and the amount of corrections made. If you printed that, you would get a space and the amount of corrections concatenated on the end. This is a place to share and to teach, I don’t care if you think I used chatgpt/didn’t write it, this code does exactly what I intended it to how I intended it to. If you have a problem with that you can make your own form post, but as I’ve explained above, I have no intention of changing it, as it helps demonstrate what gsub does to those who aren’t as familiar with it.

2 Likes

He is correct, and is using best practice. Returning extra values in the return tuple that aren’t used/intended is bad practice.

Lua/Luau supports returning multiple values without having to explicitly use a container object like most languages. So, for example, I could do:

local function returnOneMultiple()
   return "One", 1
end

If I wanted to make a function to only return “One”, I would need to throw away the second value of the tuple:

local function returnOneTextOnly()
    local oneText, oneNumber = returnOneMultiple()
    return oneText
end

Alternatively, I could use the table library to do this:

local function returnOneTextOnly()
    return table.pack(returnOneMultiple())[1]
end

Or you could use what is basically a table.pack shorthand:

local function returnOneTextOnly()
    return {returnOneMultiple}[1]
end
1 Like

Yep, could also just put it in parenthesis, but as I stated in the first message, the goal of the post is to show a bit about how gsub works, and one important returned value is the amount of changes made, so creating a variable and the underscore highlights that to those who might not know.

1 Like