First Character of String Lowercase - C#

nemo_87 picture nemo_87 · Feb 13, 2014 · Viewed 26.1k times · Source

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

For example: ConfigService

And I need it to be like this: configService

Answer

Amit Joki picture Amit Joki · Feb 13, 2014

This will work:

public static class StringExtensions
{
    [CanBeNull]
    public static string FirstCharToLowerCase([CanBeNull] this string str)
    {
        if (string.IsNullOrEmpty(str) || char.IsLower(str[0]))
            return str;

        return char.ToLower(str[0]) + str.Substring(1);
    }
}

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

Usage:

myString = myString.FirstCharToLowerCase();