I would like to split a string using multiple chars to split upon. For example, consider spin text format:
This is a {long|ugly|example} string
I would want to parse this string and split it on the “{“, “|”, and “}” chars
myString.Split('|','{','}')
Now I have tokens to play with, but what I would like is to retain the info about which char was used to split each piece of the array that is returned.
Any existing code that can do something like this?
I would tend toward using regular expressions on this. Then you could use match groups to track what matched what.
Check out this regular expression tester. Use your test data and this regular expression pattern:
This effectively splits the string into 5 matches. Each match contains 2 groups: the split string and the character it split on.
The code to run this would be something like this:
Now the match collection m will contain a Match object for each matched string, which in turn will contain 2 groups, the string and the character which was split upon.