Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you add a space after every character in a string?

2 answers

✓ Accepted answer

I don't really agree with Goulstem's solution. Forgive me for wanting better solutions.

local str = "This should have a space after every character"
local newstr = str:gsub('(.)','%1 ');

A character in this case is literally anything, meaning it will actually also insert spaces after spaces.


How does it work?

  • (.) captures any character
  • %1 stands for the character we captured
  • puts a space after it.

To add a space after every character in a string, iterate through the string using gmatch. Use the string pattern %w to indicate an iteration on any found alphanumeric character. You can see a list of string patterns here.

While iterating, start creating a new string that consists of everything the old one had, but add a space in between.

local str = "Hello my name is Goulstem" --Your String
local newstr = "" --The new string

for i in str:gmatch("%w") do
	newstr = i.." " --Add a space
end
print(newstr);

1 comment

0

String concatenation like that is really really slow.

This discussion is preserved as it was. New answers, comments, and votes are closed.