Search code examples
c#stringlowercase

First Character of String Lowercase - C#


How can I make the first character of a string lowercase?

For example: ConfigService

And I need it to be like this: configService


Solution

  • This will work:

    public static string? FirstCharToLowerCase(this string? str)
    {
        if ( !string.IsNullOrEmpty(str) && char.IsUpper(str[0]))
            return str.Length == 1 ? char.ToLower(str[0]).ToString() : char.ToLower(str[0]) + str[1..];
    
        return str;
    }
    

    (This code arranged as "C# Extension method")

    Usage:

    myString = myString.FirstCharToLowerCase();