• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 784
  • Last Modified:

Split with character \

I've just started working with C#.

I'm trying to split a string into an array at the point where the string has a slash character \  

But it seems to be a special little slash to C# because if I try to use this line

string[] arrUsername = RemoteUser.ToString().Split(("\").ToCharArray());

The text after the character turns a different color and VS tells me I'm starting a new line there ("new line in constant").

My attempts to code it alternatively as ASCII character 92 are met with "  'char' is a type but is used like a 'variable'   ", which I don't understand (background is VB.NET)

Is there another way I could code this line ?


(Here's the definition of RemoteUser, a local string I'm trying to split in that line)
string RemoteUser = Request.ServerVariables["REMOTE_USER"];
  • 2
1 Solution
Fernando SotoCommented:
Hi CAWhite;

In C# the \ in a string is an escape character. To have the compiler to ignore it in a string use the @ sign as below.

string[] arrUsername = RemoteUser.ToString().Split((@"\").ToCharArray());


CAWhiteAuthor Commented:
Thanks !
Fernando SotoCommented:
Not a problem, glad to help. ;=)

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now