Compare if string contains integer

Get help using Construct 2

Post » Tue Jan 21, 2014 2:27 pm

[QUOTE=LittleStain] Would something like this work?

Create a group and add a local variable.

set the local variable to tokencount(text.text,",")

repeat tokencount times
system compare two values : tokenat(text.text, local variable, ",") = 7
- do something
else - system subtract 1 from local variable

ok, I made an error here, I'm sure, but it should be something along the realms of this.[/QUOTE]

Its pretty good but what about integers like 12,125,1255 etc? they have more than one number in them.


Really weird, I thought it would be pretty common in construct2 and easily fixable issue.PKrawczynski2014-01-21 14:27:55
B
22
S
7
Posts: 94
Reputation: 1,783

Post » Tue Jan 21, 2014 2:30 pm

You want to check if an integer exists right?
Does it matter if it's 1243562 or 5 or 5678 or 789

you could check for each and everyone using the method I showed.


I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Jan 21, 2014 2:34 pm

you can replace the 7 in my example by any number you want or even by a changing global variable. it seperates the string into portions which are divided by the seperator "," and if that number is 23436523 or 1 it doean't matter, at least that's what i think I just put there..

I might have made a small logical mistake, but it should almost be ready for use..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Jan 21, 2014 4:25 pm

[QUOTE=PKrawczynski]

I dont want to be ungratefull but thats just not helpfull at all. I know about manual and Im reading it whenever im in doubts.

I would be very happy for complete answer with directions what to do. [/QUOTE]

To be fair, it's quite impossible for others to know for sure if someone has read the manual or not. And not all people do.

Need help? Upload .capx file with your question.
B
16
S
4
G
2
Posts: 233
Reputation: 2,560

Post » Tue Jan 21, 2014 5:03 pm

You can use a regex expression.
Using the "test regex" system condition with
"77,1,6,7,3,17" as String
"\b7\b" as Regex and
"" as Flags

It will only be true if a 7 by itself is in the string, no false positives.

Here is a reference for regex expressions:
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
B
94
S
33
G
113
Posts: 5,354
Reputation: 73,269

Post » Tue Jan 21, 2014 5:11 pm

[QUOTE=CincyPlanet] If you string returns like you show you could check for the commas also: ,7, which would not give any other numbers, like 17, 27, etc.

find(yourText,",7,")

will return > -1 if found (returns the index position)[/QUOTE]

This one will do just what you need short and effective.

the bit where the 7 is between commanas can be changed too

needle = 7
haystack = "1,2,3,4,5,6,7,8,10,17,27,"

result = find(haystack,","&needle&",")

result will be 0 (indicating it occured one)



needle = 11
haystack = "1,2,3,4,5,6,7,8,10,17,27,"

result = find(haystack,","&needle&",")

result will be -1



needle = 1
haystack = "1,2,3,4,1,5,6,7,8,10,1,17,27,"

result = find(haystack, ","&needle&"," )

result will be 2


The provided codes adds the commas before and after, but this could be changed to suite your needs.

lennaert2014-01-21 17:12:11
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Tue Jan 21, 2014 8:24 pm

Hey guys, thanks for responses I will give them a shot! Looks promising :)
B
22
S
7
Posts: 94
Reputation: 1,783

Previous

Return to How do I....?

Who is online

Users browsing this forum: dop2000 and 6 guests