Siddharth
Siddharth

Reputation: 141

how to get a substring from a string in c#?

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

Answers (1)

Sujit Singh
Sujit Singh

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

Related Questions