It seem to be printing the function of find, or the variables of it anyway, the 5 is end of cde so use string length of cde + ty-1 to get the 5 instead
the first print as shown gave me an error
so i recoded as
output.clear()
xp = "abcdef"
print(string.find(xp,"cde"))
The first and second prints are giving you numbers that denote where in the string those characters are found if you were to do this instead
output.clear()
xp = "abcdef"
print(string.find(xp,"cde"))
ty = string.find(xp,"cde")
print(ty)
print(xp)
print(string.sub(xp,ty,-2))
I think the last one would give you what you’re after. Alternatively you could just use
ty = string.match(xp,"cde")
print(ty)
ty[2] would only work if ty was a table. What youre saying when you use ty[2] is that you want to reference the table ty’s 2nd index. Since ty isn’t a table you’d get nil.
.@david19801 string.find is returning two values. The print function prints them both, but you are only capturing the first result in your ty variable.
From the string.find documentation:
Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil.
So it is returning the start index of “cde” and the end index of the match in the string “abcdef”.
If you want to capture both variables, do it like this:
start, last = string.find(xp, "cde")
print( start, last )
Yes! I just figured it out before seeing your guys’s answer! I used
ty,tz=string.find(xp,"cde");
Now ty is 3, and tz holds the 5. I remember seeing something similar with two variables = one thing in one of the examples.
Thanks for your help guys!
Now the question is, is it possible to get out just the second element alone? (just the tz in the example), without setting a ty first? like array[2] kinda…
Like:
..,tz=string.find(xp,"cde")
so set just one variable, and make it equal to the second element? basically, how to access just the second element of string.find?