PrimuS PrimuS -4 years ago 170
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

Answer Source

You can split by string, not by character:

var result = ip.Split(new string[] {" | "}, StringSplitOptions.RemoveEmptyEntries);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download