Jump to content

Go letter by letter in a string.


Recommended Posts

Posted

For example, on C I would do:

  
char test[24] 
test = "hello" 
print(test[4]) 

And if I'm not wrong that will print 'l'.

But on lua I tryed that, and seems not to work.

How can I do it?

Thanks

Posted

But what I need is:

  
test = "hello" 
outputChatBox(test[1]) -- h 
outputChatBox(test[2]) -- e 
outputChatBox(test[3]) -- l 
outputChatBox(test[4]) -- l 
outputChatBox(test[5]) -- o 

Hope you understand me.

--EDIT

Nevermind, got it, thanks!

Posted
local test = "hello" 
for i=1, #test do 
  outputChatBox(test:sub(i, 1)) 
end 

second parameter here is not the amount of characters, it is ending position.

this way it'll output only "h" and nothing after it.

string.sub(s, i [, j])

s:sub(i [,j])

Return a substring of the string passed. The substring starts at i. If the third argument j is not given, the substring will end at the end of the string. If the third argument is given, the substring ends at and includes j.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...