Split with character \

Posted on 2007-10-12
Last Modified: 2013-11-26
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"];
Question by:CAWhite
    LVL 62

    Accepted Solution

    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());



    Author Comment

    Thanks !
    LVL 62

    Expert Comment

    by:Fernando Soto
    Not a problem, glad to help. ;=)

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video discusses moving either the default database or any database to a new volume.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now