PrimuS PrimuS - 3 months ago 14
C# Question

How to split a string by multiple chars?

I have a string like this:

string ip = " | SomeName"
I want to split it by the
(including the spaces. With this code it is not possible unfortunately:

string[] address = ip.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);

as this leads to
" "
. I know I can add
but is that really the right approach?

Simply adding the spaces(
' | '
) to the search pattern gives me an

Unrecognized escape sequence


You can split by string, not by character:

var result = ip.Split(new string[] {" | "}, StringSplitOptions.RemoveEmptyEntries);