0

I have strings stored in the format "domain\alias" and I need to store only the domain name in the second string. what is the shortest way of doing this? without any loop?

3
  • How are your strings stored? You should probably at least show what you have tried. Are they just in a list? Are you getting them from the DB? Commented Sep 6, 2016 at 15:08
  • String principalname = "middleeast\v-yanivf"; Commented Sep 6, 2016 at 15:40
  • Sounds like Sujit has what you need then. Have fun. Commented Sep 6, 2016 at 15:42

1 Answer 1

2
 public string GetUserNameFromServicePrinciple(string principleName) { string userName = principleName; //if input is just username and not a valid service principle name with domain if (!string.IsNullOrEmpty(principleName)) { var splittedParts = principleName.Split(@"\".ToCharArray()); userName = splittedParts.Length > 1 ? splittedParts[1] : principleName; } return userName; } 
Sign up to request clarification or add additional context in comments.

18 Comments

this works but the only issue is that my string is principalname(a variable) so prefixing it with the symbol @ doesn't work. thanks!
how do i adjust @ here: fullName1= "middleeast\v-yanivf"; var parts = fullName1.Split('\\'); var domain = parts[0]; //check the length of array to ensure it has elements.
Hi Siddharth, var domainFOrmattedString = @"domainTest......" is just an example. In your application you must have user's principle name coming in the variable so you would not have to put @ before the variable.
Hi sunil, I tried the below code but it doesn't help: var string1 = "fareast\v-sidmis"; var parts = string1.Split('\\'); var domain = parts[0]; Console.WriteLine(domain);
Can you please edit your post and put the code in there? It will be good to see proper formatted code
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.