Search code examples
c#stringlowercase

How to convert a part of string to lowercase in c#


I have a string like this LUXOR and I want to convert other letters to lowercase except first letter or the string.that meant, I want this string Luxor from above string. I can convert full string to upper or lower by using ToUpper or ToLower.but how can I do this.hope your help with this.thank you


Solution

  • You can make use of TextInfo class that Defines text properties and behaviors, such as casing, that is specific to a writing system.

     string inString = "LUXOR".ToLower();
     TextInfo cultInfo = new CultureInfo("en-US", false).TextInfo;
     string output = cultInfo.ToTitleCase(inString);
    

    This snippet will give you Luxor in the variable output. this can also be used to capitalize Each Words First Letter

    Another option is using .SubString, for this particular scenario of having a single word input:

    string inString = "LUXOR"
    string outString = inString.Substring(0, 1).ToUpper() + inString.Substring(1).ToLower();