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:


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.

Try Split(“\\”) - You need to escape the backslash in order for it to work.

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

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:


var t = 'ONE\\\\TWO';
t = t.replace(/\\\\/g,'<backslash>');
t = t.split('<backslash>');
alert(t);

use single quotes to tell it its a 'c’haracter rather than a “string”

  
 string[] arrResult = strUsername.Split('\\\\');

on a side note i wish @‘\’ was valid code

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