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

How to split string into individual characters?

So, i would like to make:

''Hey dude!''

into

''HeY DUdE'' (or similar)

using a script.

Using my pre-existant knowledge of how to do this in python, for the first step, i tried splitting a string into characters, and place them into an array:

text = 'Hey dude!'
tt = {}
for i = 1,#text do
    table.insert(tt, text[i])
end

for i = 1,#tt do --Debug
    print(tt[i])
end

However, this script will not fill the list.

Any ideas on how to make this work would be great. Thanks.

1 answer

✓ Accepted answer

you have the right idea, but you're executing it wrong. text doesn't support keys that are meant to point to a character in a string. instead, use textObject:sub(n, n). using the same number for the start and end point will allow to capture the character that has the same position value as n.

for example:

local String = "Hello, World!";
print(String:sub(1,1))
--//H

5 comments

0

note that whitespace is also a character, too. if you want, you can gsub whitespace out. enjoy.

0

Thanks. I also never fully knew that str:sub(1,1) would work, thx.

0

wAIT AMRK AS ANASWER

0

i'll do it cuz i'm so nice

0

THANKS INCAPAAAAAZ

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