anonym0use
anonym0use

Reputation: 3046

string split based on char \

Got a bit of a mind freeze at the moment. I have the following syntax:-

        string[] repname = Thread.CurrentPrincipal.Identity.ToString().Split('\');

and I get an error on the split character. Can anyone advise on how I can do the split using the \ character as the delimiter?

Cheers

Upvotes: 2

Views: 594

Answers (5)

Sampson
Sampson

Reputation: 268344

Typically, the \ character is meant to escape other characters. If you wish it to be taken literally, you need to escape it with another \. So, in order to escape on backslashes, you'll provide \\.

Upvotes: 1

Micah
Micah

Reputation: 116090

\ is an excape character in C#.

You need to do one of the following:

Split('\\');

or

Split(@'\');

Upvotes: 1

Maiku Mori
Maiku Mori

Reputation: 7469

Use

Split('\\')

"\" is an escape character.

Upvotes: 16

scottm
scottm

Reputation: 28701

 string[] repname = Thread.CurrentPrincipal.Identity.ToString().Split(new string[]{"\\"}, StringSplitOptions.None);

Upvotes: 0

Stu Mackellar
Stu Mackellar

Reputation: 11638

Split takes a char[] as a parameter, not a char. Try;

string[] repname = Thread.CurrentPrincipal.Identity.ToString().Split(new char[] {'\\'});

Upvotes: 1

Related Questions