How do I check, if string contains another string?

Get help using Construct 2

Post » Thu Dec 01, 2016 9:30 pm

Hello everyone, simple question:

I have a string variable, let's call it "My name is Peter". I am going to store this variable in an array at some random XYZ place.

Now I want to check whether the whole array contains the string "Peter". I haven't found any solution to this.

Any ideas? maybe I need a plugin?
B
21
S
9
G
2
Posts: 128
Reputation: 3,001

Post » Fri Dec 02, 2016 6:40 pm

Contains value
Searches the entire array to check if any of the elements contains the given value. For example, you can use this to test if the string "sword" is stored anywhere in the array.
B
47
S
18
G
84
Posts: 2,273
Reputation: 49,800

Post » Sat Dec 03, 2016 3:23 pm

Yes, but if the string in the array is 'I have a sword', I won't find it by checking every element for the string 'sword', as it has to be a perfect match.

I guess the expression 'regex' is exactly what I need. I'll try it out.
B
21
S
9
G
2
Posts: 128
Reputation: 3,001

Post » Sat Dec 03, 2016 3:59 pm

Local variable 'check' = 0

Iterate the array with 'For each element'.

Compare two values ... find(Array.CurValue,"sword") not = - 1
If that condition is true then the expressions Array.CurX, Array.CurY and Array.CurZ contain the position in the array.
And CurValue contains the string
Optional you can stop the iteration with the action stop loop at this point

If you want to know all occurrences, then you need an array to hold the positions.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat Dec 10, 2016 3:08 pm

Works. Thanks!
B
21
S
9
G
2
Posts: 128
Reputation: 3,001


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 27 guests