user3227070 user3227070 - 1 year ago 69
C# Question

C# splitting a string with multiple seperator

user1;user2;user3;user4 user1


I'd like to split these strings so I can iterate over all of them to put them in objects. I figured I could use

myString.split(";")


However, in the second example, there is no ; , so that wouldn't do the trick. What would be the best way to do this when it can be variable like this?

Thanks

Answer Source

You can use overload taking multiple separators:

myString.Split(new[] { ";", " " }, StringSplitOptions.RemoveEmptyEntries);