How would I script my own Format Function?

Hello,

I am not necessarily working on a game but I would love to learn a bit more about roblox programming.
So my question is how I would create my own format function.

It would go like this, for example:

Label.Text = "Welcome {} to {}. My name is {}!":format(Player.Name, "Jailbreak", "David")
--or
Label.Text = format("{}, woah so {}", God, Cool)

I would prefer to use {} instead of like %d etc.

I am excited to see what you come up with!

Thanks in Advance!

1 Like

You could string.split by {} and concatenate with the arguments you give it.

You could string.gsub to replace {} with %s and then use the built in formatter.

Or you could use string.gmatch to match everything between each set of {} and then again concatenate with the arguments you’ve given.

Whatever floats your boat.

Though it seems a pointless exercise to write your own formatting function. If it’s for problem solving and scripting practice it makes more sense to try to come up with the solution yourself, otherwise it’s just practice for everyone else on the forum and all you do is implement what we’ve told you.

3 Likes

I used gsub to try to replace the {} with %s’s but it gives me a warning that I can’t use %s.

Is there another way I could do that, or/and have I dont something wrong?

local GSubbed = string.gsub(Msg, "{}", "%s")
local Formatted = GSubbed:format(...)

image

1 Like

Try %%s to escape the % sign. It’s just due to it being confused with patterns.

2 Likes