This forum is currently in read-only mode.
  • I don't want to resort to Python if I can help it (I'm having to do that more often than I'd like), but I thought I'd just check with you guys first, just in case it is buried somewhere in Construct.

    What I need to do is get the number of matches between a delimiter-separated text variable "a,b,c,d,e" etc, and a given text like "a".

    I can obviously do this with events, but what I need is to be able to use it within a condition, like

    If FindToken(Global('TextVariable'),"a",",")>0

    Which returns true if one or more "a" is found in the Variable.

    I can see Get Token and Get Token Count in there, but I can't see a FInd Token or equivalent.

    Is there one in there?


  • It depends on the details. For the specific example, there's

    Find(source, string [, start])

    It doesn't care about delimiters, but that would not be needed for it anyway.

    So, a call to

    + Find(Global('TextVariable'), "a") > 0

    -> do stuff if a is in text

    will return the first index of occurance or 0

  • See, I wouldn't have looked for "Find".

    I was busy looking at the various text functions for it.

    This will work perfectly for what I want.

    Thanks again mate.

    BTW, I notice that the index returned for the very first letter in the string is 2, instead of what I presumed would be zero.

    It doesn't affect what I need to do with Find, as I don't need the returned value, but just out of curiosity, anyone know why?


  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • A string is a string of characters and a character is not just letters or numbers or special characters like comma or quotation mark. The plain ASCII set Construct works with also contains non-printing control chars like backspace or carriage return ( You won't see those chars although they are in the string and therefore count if you use a function like 'find'.

    Why do I tell that? Well, the 'find' expression works correctly. find("a,b,c,d,e", "a") will return 1. If you encounter something else, then your source string does contain a char before the assumed first char. If you load the string from a file, make sure there is no cr or tab or another of those control chars before the first printing char. Sometimes a space is also overseen.

    p.s. the first index is 1. If the substring couldn't be found in string, zero is returned.

  • It was an error in my code that was returning the wrong index.

    There's no erroneous characters in my strings as they are assembled in the program rather than from a file.

    I finally decided to use the function object to write a function that I can pass the strings to which will then return the amount of matches found.

    The main reason for this is that I do need to use delimiters for some of the strings, as there are entries of different lengths in some of them, and using the function object allows me to use the Get Token and Get Token Count to loop through varied lengths of text and get accurate results with the matches.

    So far so good.



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