search text within text

Get help using Construct 2

Post » Fri Oct 18, 2013 4:45 pm

Hi, I have a sentence in a text object ,i want to check if the sentence has a certain word in it. im pretty sure I need to use tokenat but don't understand it could anyone help explain it simply with an example

example:


I went to the shops and bought coffee,tea,cakes and biscuits.


I would like to see if the sentence has the word tea in it


thanks
B
14
S
6
G
3
Posts: 83
Reputation: 4,228

Post » Fri Oct 18, 2013 5:41 pm

In this case I don't think you need tokenat, just find()

find(sentence, "tea") will return the index of the first time it finds "tea" and -1 if it doesn't find it at all.

If you just wanted an event to check if it existed or not, you could do System-> Compare two values.The first value would be find(sentence, "tea"), then check if it is NOT equal to -1. The event will fire if the text is found.

EDIT: Note, you have to be careful with this, because let's say you had the word "team" in your sentence. It would find "tea" and then return that it found something. Wertle2013-10-18 17:49:32
B
11
S
2
Posts: 87
Reputation: 1,112

Post » Fri Oct 18, 2013 5:45 pm

Just to follow up, tokenat() is used if you want to break up a string that is delineated by a common character. So for example, if you had a string that was like "coffee,tea,cakes,biscuits," that is delineated by commas.

If you wanted to get, say, the third item in that list, you would do tokenat(list, 2, ","). That says "get me the third (remember indexing is 0-based, so it's 2 and not 3) object in the given string where the objects (or tokens) are separated by a comma"
B
11
S
2
Posts: 87
Reputation: 1,112

Post » Sun Oct 20, 2013 12:16 pm

Thanks very much Wertle, I used find and works fine also understand the tokenat too, much appreciated :)
B
14
S
6
G
3
Posts: 83
Reputation: 4,228


Return to How do I....?

Who is online

Users browsing this forum: ST314 and 34 guests