How do I parse a string?

  • Hey everyone,

    not a coder, so I'm prolly abusing terminology again

    I want to have a text var on an object that I can fill with level design data in the editor and iin runtime treat it according to its setup data.

    e.g. have a variable 'spawnPhase' = "0,1,2" and during individual 'phases' of the map check if the object is currently meant to be spawned.

    so how can i get an event to look for the occurence of a string within a string?

  • There are a number of expressions you can use:

    to parse a string with a list of values separated by comma or some other symbol - use tokenat(), tokencount()

    For example:

    For "x"=0 to tokencount(text, ",")-1

    ...value = int(tokenat(text, loopindex, ","))

    to search for a substring - find()

    Other expressions:

    left

    right

    mid

    len

    etc.

    https://www.scirra.com/manual/126/system-expressions

  • dop2000 Cool, thanks!

    I'm now struggling to cope with the variable types ...

    I'd like to treat my substring as a number but look for it within a string. So like finding the number 1 inside the string "0,1,2".

    Not sure about the syntax it requires.

    Should find(Object.myStringOfNumbers,numberInQuestion) work?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • facecrime

    find(Object.myStringOfNumbers, "2")

    If this returns a number >=0, this means that "2" is found.

    But, if your string is long and contains numbers with multiple digits ("0,7,8,12,22,729") , if you need to find exact number 2, you should search for ",2," string.

    With lists like this it's better to use tokenat/tokencount

  • dop2000

    ah right, i will only have single digit numbers, so "2" or ",2," would both do.

    my issue is probably that i want to pass in a variable instead of the actual value in quotes. but the variable type is number.

    does it need converting then somehow or how do get it to interpret it as a text type ?

  • You can convert both ways:

    Set number=int("2")

    Set text=str(2)

  • Cheers dop2000 !

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