How can I get the position of a letter in a string. EX:
a = "x x x"
I would like to get the position of each x. I tried using the string.find, but it just picked up the first one.
How can I get the position of a letter in a string. EX:
a = "x x x"
I would like to get the position of each x. I tried using the string.find, but it just picked up the first one.
a = "x x x"
pos = 0
while 1 do
pos = a:find( "x", pos + 1 )
if not pos then break end
print( pos ) -- poderia armazenar numa tabela
end
Output:
1
4
7
See working at IDEONE .
Try also
a = "x x x"
for k in a:gmatch("()x") do
print(k)
end
Note the empty catch ()
, which captures the position.