Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string split based on char \

Tags:

c#

c#-3.0

c#-2.0

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

like image 463
anonym0use Avatar asked Dec 06 '25 06:12

anonym0use


2 Answers

Use

Split('\\')

"\" is an escape character.

like image 116
Maiku Mori Avatar answered Dec 07 '25 22:12

Maiku Mori


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

string[] repname = Thread.CurrentPrincipal.Identity.ToString().Split(new char[] {'\\'});
like image 38
Stu Mackellar Avatar answered Dec 07 '25 20:12

Stu Mackellar