How do I delete token in a string?

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
  • I have a long string of tokens, let's say "A;B;C". I want to select one at random and then remove it from the string, so that when I select another at random, it can't select the one that's already been picked. So if "B" gets selected, it becomes "A;C".

    First goto was replace(string, token&";", "") and this works well, except for when the last token is picked ("C" in the example). Since it doesn't have ";" after it, it doesn't get deleted by the replace function. I had to make a whole different check for if the token selected is the last token in the line.

    Just want to make sure I'm not overthinking/engineering this thing. Am I on the right track?

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I’d say what you're doing is fine. You could also add semicolons to the front and back of the string to be able to have one replace work for all positions. Then you could remove the semicolons at the ends with mid() but usually I leave them there if I’m doing that sort of thing.

    Do bear in mind that if two or more tokens are the same then the replace will remove them all.

    A general solution could be to make your own tokenat that returns the index of a token. And then you can remove that with a left and right I imagine.

  • Adding a separator at the end for the replace function and then removing it with right seems like it was the simplest solution. Thanks for the tip!

    Though I'm curious as to why you said you'd leave the separators at the beginning and end? Wouldn't that create 2 extra blank tokens?

  • It would but you can keep that in mind when using tokenat. It’s just a complexity trade off.

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