0 Favourites

Compare if string contains integer

  • Hey, thats something terribly easy yet Im having hard time doing it with construct2.

    "1,6,17,3" - lets say I need to check if it contains number 7. I cant just check if 7 is in string because there is very high chance of false positive. I did want to do what I was used to in any programming language wchich means splicing text by , separator and put all results into array, then check if value is contained in array but in construct2 its just awfully messy, long, and pretty ugly.

    Any ideas how I can do that faster/simpler way?

  • In the manual you can find system expressions you can use with text-objects.

  • 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)

  • In the manual you can find system expressions you can use with text-objects.

    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.

  • 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.

    No, actually you cant. You cant ever assume its going to be written that way since there are 4 ways to do it.

    7 will get false positive with 17

    ,7 will get false positive with ,71

    7, will get false positive with 17,

    ,7, will never get false positive but there is only 25% chance it will have this format instead of other 3.

  • Maybe you could also check before and after the 7? If there is a comma or nothing (index value 1 or text length -1)?

  • Maybe you could also check before and after the 7? If there is a comma or nothing (index value 1 or text length -1)?

    And what about checking integers like 25,125,1257,13643 etc? as you see this solution doesnt scale very well and I have to dismiss it. I did thought about that.

  • I see, tough one...Well, I think, you would only have to check the first index: 7, and the last one.

    Looking at the expressions available maybe these could help also. You could use the comma separator, check the first and last value, and info not found use the ,7,?

    tokenat(src, index, separator)

    Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.

    tokencount(src, separator)

    Count how many tokens occur in src using separator. For example, tokencount("apples|oranges|bananas", "|") returns 3.

    Im not sure if you could send the list to an array and then cycle through that also...just kind of brainstorming at this point.

  • 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.

  • 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.

    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.

  • 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.

  • 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..

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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.

    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.

  • 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

  • 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)

    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.

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