Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove domain information from login id in C#

Tags:

I would like to remove the domain/computer information from a login id in C#. So, I would like to make either "Domain\me" or "Domain\me" just "me". I could always check for the existence of either, and use that as the index to start the substring...but I am looking for something more elegant and compact.

Worse case scenario:

int startIndex = 0; int indexOfSlashesSingle = ResourceLoginName.IndexOf("\"); int indexOfSlashesDouble = ResourceLoginName.IndexOf("\\"); if (indexOfSlashesSingle != -1)     startIndex = indexOfSlashesSingle; else     startIndex = indexOfSlashesDouble; string shortName = ResourceLoginName.Substring(startIndex, ResourceLoginName.Length-1); 
like image 744
Dan Avatar asked Oct 09 '08 01:10

Dan


2 Answers

when all you have is a hammer, everything looks like a nail.....

use a razor blade ----

using System; using System.Text.RegularExpressions; public class MyClass {     public static void Main()     {         string domainUser = Regex.Replace("domain\\user",".*\\\\(.*)", "$1",RegexOptions.None);         Console.WriteLine(domainUser);        }  } 
like image 182
user26350 Avatar answered Sep 18 '22 17:09

user26350


You could abuse the Path class, thusly:

string shortName = System.IO.Path.GetFileNameWithoutExtension(ResourceLoginName); 
like image 35
Jeffrey L Whitledge Avatar answered Sep 20 '22 17:09

Jeffrey L Whitledge