Identify the characters at every position in a string?

Hi everybody,

Another painfully dumb question from yours truly.

I checked the forums as well as the help documentation but couldn’t find what I was looking for (or, more likely, I did, but couldn’t understand how it applied to my situation).

I’m trying to iterate through a string and identify the character at each position in the string but I can’t seem to wrap my dense brain around how to do that. Could someone humor me and help me out?

e.g. I have the string “1—1–1” and would like to go through each character, and if there is a ‘1’ at any position, stop and draw an object (that corresponds to the particular position in the string).

Most of my experience is in animation and not so much string manipulation so this is foreign territory to me. Your help is appreciated…

Nick [import]uid: 7472 topic_id: 8760 reply_id: 308760[/import]

Sure thing,

Here is how you can access each character in a string…

testString = “Cool”

for i=1, string.len(testString) do
– use string.sub to get a substring of one character…
– at position i
local stringCharacter = string.sub(testString, i, i)

– if we find the letter “o”, then we print a message
if(stringCharacter == “o”) then
print(“found the letter o :)”)
end

end
Enjoy :slight_smile: [import]uid: 12700 topic_id: 8760 reply_id: 31952[/import]

Brilliant.

Thank you! [import]uid: 7472 topic_id: 8760 reply_id: 31960[/import]