acadia
acadia

Reputation: 2333

IndexOf function

I have this below code which will fetch loggedin UserID

System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;
        //networkID=User.
        string strName = p.Identity.Name;
        int start;
        start = strName.IndexOf("\") + 1 ; 

  strName = strName.Substring(start, strName.Length - start);

But start = strName.IndexOf("\") + 1 ; throwing out error saying Newline in constant. The logged in username I get is domainName\username and I want to use only username. Please help

Upvotes: 0

Views: 3857

Answers (5)

Will Vousden
Will Vousden

Reputation: 33408

\ is an escape character. You either need to escape it with another \ or use verbatim string literals:

start = strName.IndexOf("\\") + 1 ;

Or:

start = strName.IndexOf(@"\") + 1 ;

Upvotes: 0

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

Your backslash is escaping your last quote. use a double backslash to escape your backslash.

start = strName.IndexOf("\\") + 1 ; 

Upvotes: 0

Michael Stum
Michael Stum

Reputation: 181104

You have to escape your backslashes by doubling them:

start = strName.IndexOf("\\") + 1 ; 

Upvotes: 1

David M
David M

Reputation: 72930

\ is an escape character - you must use either "\\" or @"\" to get this to work.

Upvotes: 5

Ariel
Ariel

Reputation: 5830

Try IndexOf(@"\") or IndexOf("\\").

Upvotes: 6

Related Questions