Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi,

I need to filter everything before the backslash. For example domain\username, domain and the backslash must be filtered and removed.

I tried this but it aint working.

var x = IPGlobalProperties.GetIPGlobalProperties().DomainName.ToUpper();
string s = Regex.Replace(x, @"^\s*(.*?)\s*$", "");


Thanks in advance!
Posted

u can do like this also

C#
string s = "domain\\username";
       string a = s.Substring(s.IndexOf("\\")+1);



Hope it helps
 
Share this answer
 
Comments
Member 7762422 11-Apr-11 5:20am    
Thanks all, Ashishmau your solution worked in my situation thanks allot
MIDL
Find a character is present in string or not
            string abc = "anvas the real napster /";
            char[] SpecialChars = "*-_= /".ToCharArray();
            int indexOf = abc.IndexOfAny(SpecialChars);
            if (indexOf != -1)
            {
                //there should a special char in the string
                // if u want to find out the character
                          string charval = abc[indexOf].ToString();
            }
            else
            {
                //string is fine
            }
 
Share this answer
 
Hi,
Try this,

C#
char[] separator = new char[] { '\\' };
            string[] new_x= x.Split(separator,3);


It will split ur String ( i.e x)into 3 parts i.e new_x[0],new_x[1],new_x[2].
check which string of array contains ur answer and just use it.

I think in ur case new_x[1] will hold ur username.


Or
refer this link
[^]
 
Share this answer
 
v2
Comments
Member 7762422 11-Apr-11 5:01am    
I've got this now:

string abc = "Domain\\Username THE NAPSTER";

char[] separator = new char[] { '\\' };
string[] new_abc = abc.Split(separator, 3);

But it aint filtering everything before backslash so domain must be removed
Pratik Bhesaniya 11-Apr-11 5:08am    
new_abc[1] holding ur output ===>> Username THE NAPSTER
and i think this is what u want or try below,
Response.Write(new_abc[0] + "" + new_abc[1]);
check what it should gives you as output .
If you want to remove all characters from beginning of the line upto the first "/" in front of the part you want to keep (and including "/") this is your regex:
Regex.Replace(x, @"^.*\/(.*$)","$1");
 
Share this answer
 
v3
C#
string s = System.IO.Path.GetDirectoryName(x);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900