22

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

For example: ConfigService

And I need it to be like this: configService

Andrew Backes
  • 1,884
  • 4
  • 21
  • 37
nemo_87
  • 4,523
  • 16
  • 56
  • 102
  • 1
    Did you check this out? http://stackoverflow.com/questions/3565015/bestpractice-transform-first-character-of-a-string-into-lower-case – Niklas Feb 13 '14 at 13:34
  • 1
    http://stackoverflow.com/questions/4135317/make-first-letter-of-a-string-upper-case Instead `ToUpper` change it to `ToLower`. – Random Feb 13 '14 at 13:35
  • 3
    Note: anything "first character" related will be very dangerous when talking about anything outside simple unicode. Combining diacritics, conjugate pairs, and right-to-left will all cause it problems – Marc Gravell Feb 13 '14 at 13:37

9 Answers9

57

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();
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
21

One way:

string newString = oldString;
if (!String.IsNullOrEmpty(newString))
    newString = Char.ToLower(newString[0]) + newString.Substring(1);

For what it's worth, an extension method:

public static string ToLowerFirstChar(this string input)
{
    if(string.IsNullOrEmpty(input))
        return input;

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

Usage:

string newString = "ConfigService".ToLowerFirstChar(); // configService
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
8

You could try this:

lower = source.Substring(0, 1).ToLower() + source.Substring(1);
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
2
    string FirstLower(string s)
    {
        if(string.IsNullOrEmpty(s))
            return s;
        return s[0].ToString().ToLower() + s.Substring(1);
    }
nima
  • 6,566
  • 4
  • 45
  • 57
2

Use this function:

public string GetStringWithFirstCharLowerCase(string value)
{
    if (value == null) throw new ArgumentNullException("value")
    if (String.IsNullOrWhiteSpace(value)) return value;

    char firstChar = Char.ToLowerInvariant(value[0]);

    if (value.Length == 1) return firstChar;

    return firstChar + value.Substring(1);
}

Please note that further overloading will be necessary if support for other languages is required.

Crono
  • 10,211
  • 6
  • 43
  • 75
2
    public static string Upper_To_Lower(string text)
    {
        if (Char.IsUpper(text[0]) == true) { text = text.Replace(text[0], char.ToLower(text[0])); return text; }

        return text;
    }

    public static string Lower_To_Upper(string text)
    {
        if (Char.IsLower(text[0]) == true) { text = text.Replace(text[0], char.ToUpper(text[0])); return text; }

        return text;
    }

Hope this will help you ! here i made two methods taking as parameter any string and return the string with the first letter uppercase or lowercase according to the method you will use

Antho
  • 181
  • 11
1
string test = "ConfigService";
string result = test.Substring(0, 1).ToLower() + test.Substring(1);
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • 2
    Also, using `Substring` method just for getting first char seems a little bit over the top :) – Tarec Feb 13 '14 at 13:38
1

I would simply do this:

Char.ToLowerInvariant(yourstring[0]) + yourstring.Substring(1)

Simple and gets the job done.

EDIT:

Looks like this thread had the same idea. :)

Community
  • 1
  • 1
Andrew Backes
  • 1,884
  • 4
  • 21
  • 37
1

This can help you,changes first character to lower if it is upper and also checks for null or empty and only whitespace string:

string str = "ConfigService";
string strResult = !string.IsNullOrWhiteSpace(str) && char.IsUpper(str, 0) ? str.Replace(str[0],char.ToLower(str[0])) : str;
terrybozzio
  • 4,424
  • 1
  • 19
  • 25