0 Favourites

Quick question: 'contain' string

  • Hello peoples.

    A quick question. I'm experimenting with a 'terminal game'. I want the game to compare what's in a text input box, but how do I get it so that Construct checks to see if what's in the input box contains a word.

    Here's an example:

    I want the Construct to check if the input box contains the word "sit"

    The user enters "sit up"

    How do I get Construct to recognize that "sit up" has "sit" in it?

    • Thanks
  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • From the manual on system expressions:

    find(src, text)

    Find the first index within src that text occurs, else returns -1.

    So, you can use find(textbox.text, "sit") and if it comes up with anything other than -1, the word sit is in there somewhere. Unfortunately, it would also find any words containing "sit", such as situation. If you wanted to make sure it was a specific word you could go with:

    for "loop" 0 to tokencount(textbox.text, " ")

    -Compare to values: tokenat(textbox.text,loopindex("loop")," ") = "sit"

    That would break the string up by spaces (i.e. words, if the person isn't a bad typist like I am) and check each piece individually.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
353 32,079
yojona's avatar
yojona
Unread sticky
192 1,337,408
Kyatric's avatar
Kyatric Construct Team Support
Unread hot topic Locked topic
67 21,583
Ashley's avatar
Ashley Construct Team Founder