Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to split string into individual characters?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago

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
0
note that whitespace is also a character, too. if you want, you can gsub whitespace out. enjoy. Fifkee 2017 — 4y
0
Thanks. I also never fully knew that str:sub(1,1) would work, thx. TigerManGamingYT 2 — 4y
0
wAIT AMRK AS ANASWER Fifkee 2017 — 4y
0
i'll do it cuz i'm so nice User#24403 69 — 4y
0
THANKS INCAPAAAAAZ Fifkee 2017 — 4y
Ad

Answer this question