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

Iterate through a string's text?

Asked by
AZDev 590 Moderation Voter
8 years ago

If I have a string is it possible to iterate the each character in the string and print it one at a time?

For instance:

I have a string as a variable local message = "Welcome"

Now I want it to print.

 "W"
 "E"
 "L"
 "C"
 "O"
 "M"
 "E"

Instead of printing, "Welcome"

How would I do this?

Edit: Moved some text to a code block to preserve formatting.

0
I figured it out but will still accept the first correct answer. AZDev 590 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

An easy application of gmatch will allow you to do this. Just use "(%w)" as the string pattern

EX

for w in string.gmatch("Pizza", "(%w)") do -- 'w' represents the individual letter returned
    print(w)
end
1
Replace "Pizza" with "Welcome" if you aren't hungry randomsmileyface 375 — 8y
1
Use "." instead of "%w" if you want to include other kinds of characters (like spaces and punctuation) in the iteration BlueTaslem 18071 — 8y
0
Good point randomsmileyface 375 — 8y
Ad

Answer this question