Click to See Complete Forum and Search --> : Removing prefix XXX\username


macupryk
November 4th, 2005, 10:01 AM
:blush: I need to remove the prefix(domain name) if exist in front of a username from a session variable.


paramsql.Value = Session("ssNtUser")

say the domain is za

username is macupryk

I would like to remove the za\ so in the session variable I have

macupryk only but the domain name can change. Also there might not even be a domain name with a slash.


Thanks.

mmetzger
November 4th, 2005, 10:16 AM
This should do it...



string tmp = "mydomain\myuser";

int index = tmp.IndexOf(@"\");

if (index != -1)
{
tmp = tmp.Replace(tmp.Substring(0,index+1),"");
}



Basically, you find the first instance of "\", then find the substring of the total that includes the domain and the "\" (hence the +1). Once you have that, just do a replace and change the domain\ to nothing.

The place where this will break is with multiple \'s. To handle that, you could use the String.LastIndexOf(@"\") which would work the same way.