How do I get the index of a token?

0 favourites
  • 5 posts
  • Hi!

    I want to use this expression in reverse.

    tokenat(src, index, separator)[/code:njljj61c]
    
    My string is "text1,text2,text3".
    Now I want to get the index of "text2". Is there a way to do this with an expression?
    Or do I need to make a function on my own for this?`
    
    Thank you!
    
    Regards
    Andy
  • You'll need to do this manually. One simple idea is to use a loop like:

    Number index=-1

    Repeat tokencount(text, ",") times

    Tokenat(text, loopindex, ",") = "fish"

    --- stop loop

    --- set index to loopindex

  • You'll need to do this manually. One simple idea is to use a loop like:

    Number index=-1

    Repeat tokencount(text, ",") times

    Tokenat(text, loopindex, ",") = "fish"

    --- stop loop

    --- set index to loopindex

    Thank you R0J0hound

    I'll try that!

  • My version is just a variant of what R0j0hound already proposed.

    Simply

    For "index_name" from 0 to tokencount("text1,text2,text3",",") - 1

    --- (condition) tokenat("text1,text2,text3",loopindex("index_name"),",") = "text2"

    --- (Action) Collect dictionary: Add key "Index_var" with value loopindex("index_name")

    *replace dictionary, index_var, index_name and string as you wanted

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • My version is just a variant of what R0j0hound already proposed.

    Simply

    For "index_name" from 0 to tokencount("text1,text2,text3",",") - 1

    --- (condition) tokenat("text1,text2,text3",loopindex("index_name"),",") = "text2"

    --- (Action) Collect dictionary: Add key "Index_var" with value loopindex("index_name")

    *replace dictionary, index_var, index_name and string as you wanted

    Thank you for your example

    Thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)