Replace A Value In A String That is Separated By Comma ","

0 favourites
  • 4 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • Hi there,

    Is there any way to Replace a Value inside a string that is separated by a comma ","

    Example:

    Global Variable as a Txt

    Prizes: "1, 3, 5, Top, 6, 6, 6"

    if I need to change the index 4 which is equal to the first 6 and swap it with 9

    The Result will be >>> Prizes: "1, 3, 5, Top, 9, 6, 6"

    I tried already with Expressions (Replace and Regex) but I couldn't get it to work and I couldn't find the answer searching on the Forum either

    I have been a few days with this but no luck I tried things like

    replace(Prizes, tokenat(Prizes, 4, ",") , "9")

    Thanks

  • One idea would be to loop over the tokens and rebuild the comma separated list.

    Var a=“1,2,3,4,5”

    Var b=“”

    Repeat tokencount(a,”,”) times

    — loopindex=2

    ——add 44&”,” to b

    — else

    ——add tokenat(a,loopindex,”,”)&”,” to b

  • You can break the string into tokens and re-assemble again:

    variable newPrizes=""
    For x=0 to (tokencount(Prizes, ",")-1)
     if loopindex=4 : Set newPrizes to newPrizes & "9"
     else : Set newPrizes to newPrizes & tokenat(Prizes, loopindex, ",")
    
    
     // add comma after each token, if not the last one
     if loopindex<>(tokencount(Prizes, ",")-1) : Set newPrizes to newPrizes & ", "
    
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • R0J0hound dop2000 Awesome Ideas Thank you guys I really appreciate your help

    I was really hoping to avoid any extra loops as the problem is that I have to modify many variables at the same time using a Loop quite often, not just one and with this, I have to create a Loop inside a Loop for each variable it will be a bit overkill

    Dop2000 thanks for the Link I did run some testing with Regex but it doesn't work for me is probably me that I haven't got much experience with Regex I just starting experimenting with it the last two days I have no idea how it works though.

    If any of you have any Idea how to make it work with regex will be awesome

    This is what I got so far using the example on the link

    Capx:

    https://www.dropbox.com/s/mj3380lz01womdq/Regex%20Problem1.capx?dl=0

    Thanks Again

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