Question

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

Was it helpful?

Solution

Use

Split('\\')

"\" is an escape character.

OTHER TIPS

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

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

\ is an excape character in C#.

You need to do one of the following:

Split('\\');

or

Split(@'\');

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 \\.

 string[] repname = Thread.CurrentPrincipal.Identity.ToString().Split(new string[]{"\\"}, StringSplitOptions.None);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top