SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C# Split method using '\' causing error

    Hello - I am trying to split a string using the back slash '\' as the separator.

    I am getting this error:

    Code:
    Compiler Error Message: CS1010: Newline in constant
    
    Source Error:
    
     
    
    Line 305:            strUsername = HttpContext.Current.Request.ServerVariables["LOGON_USER"]; 
    Line 306:
    Line 307:            string[] arrResult = strUsername.Split("\");
    Line 308:            Response.Write(arrResult[1]);
    Line 309:            Response.End();
    The C# compiler thinks I am trying to create a newline.

    How can I get the string to seperate into array parts using a back slash as a separator?

    I've Googled and Googled but cann ot find an answer.

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try Split("\\") - You need to escape the backslash in order for it to work.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Jimmy, I tried that actually and got this error:

    Compiler Error Message: CS1502: The best overloaded method match for 'string.Split(params char[])' has some invalid arguments

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what causes the error, but you could try another method. I am not sure of the functions required in C# but in javascript you could replace the backslashes with something and then split with that "something" as a parameter:

    Code javascript:
    var t = 'ONE\\TWO';
    t = t.replace(/\\/g,'<backslash>');
    t = t.split('<backslash>');
    alert(t);
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use single quotes to tell it its a 'c'haracter rather than a "string"

    Code:
      
     string[] arrResult = strUsername.Split('\\');
    on a side note i wish @'\' was valid code

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the answer and I thought that I would share with anyone who is interested:

    Captured a string with a DOMAIN\UserName
    Replaced the backslash separator like so: strUserName.Replace("\\", ",");
    Created an array with two parts [0], [1]: string[] arrResult = strUserName.Split(new Char[] {','});
    Wrote it to the page to QA the result:
    Response.Write(arrResult[1]);
    Response.End();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •