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?
- 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?KSib– KSib2016-09-06 15:08:38 +00:00Commented Sep 6, 2016 at 15:08
- String principalname = "middleeast\v-yanivf";Siddharth– Siddharth2016-09-06 15:40:26 +00:00Commented Sep 6, 2016 at 15:40
- Sounds like Sujit has what you need then. Have fun.KSib– KSib2016-09-06 15:42:29 +00:00Commented Sep 6, 2016 at 15:42
Add a comment |
1 Answer
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; } 18 Comments
Siddharth
this works but the only issue is that my string is principalname(a variable) so prefixing it with the symbol @ doesn't work. thanks!
Siddharth
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.
Sujit Singh
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.
Siddharth
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);
Sujit Singh
Can you please edit your post and put the code in there? It will be good to see proper formatted code
|