Reputation: 141
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?
Upvotes: 0
Views: 93
Reputation: 829
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;
}
Upvotes: 2