Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Easy string.find question

edited March 2013 in General Posts: 43

Hello, problem:

xp = "abcdef"
print(string.find(xp,"cde")
ty = string.find(xp,"cde")
print(ty)

Now the first print shows two values. The second print shows just one value. For the exact same thing.

Questions:

Why do they do something difference?

How do I access the SECOND value in string.find as a variable (like the second print)? I tried ty[2] and it didn't work...

Tagged:

Comments

  • Posts: 1,595

    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

  • edited March 2013 Posts: 4

    .@david19801

    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.

  • SimeonSimeon Admin Mod
    edited March 2013 Posts: 5,054

    .@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 )
    

    Edit: changed end -> last

  • Posts: 1,595

    It returns a Boolean as well though if you have 4 args

  • edited March 2013 Posts: 4

    .@Simeon what you'd get if you did that is error unexpected symbol near 'end'

  • edited March 2013 Posts: 43

    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?

  • SimeonSimeon Admin Mod
    Posts: 5,054

    .@Sherrpen good point, that will teach me to use keywords as variable names :)

  • Posts: 4

    .@Simeon still it is a good template for thinking of how to use variables in their place.

  • edited March 2013 Posts: 502

    I can think of two ways, to get only the second returned element:

    1) assign a useless key and use only the second, logical one:

    _, tz=string.find(xp,"cde")
    print(tz)

    2) using from table:

    tz={string.find(xp,"cde")}
    print(tz[2])
    

    (untested)

  • .@se24vad tested and this is what you get

    error: [string "-- t ex..."]:8: bad argument #1 to 'find' (string expected, got nil)

  • SimeonSimeon Admin Mod
    Posts: 5,054

    .@BridgetMoris you have to use @se24vad's code in the context of the original poster, that is, xp needs to be a variable declared as follows:

    xp = "abcdef"
Sign In or Register to comment.