How to lowercase a string except for first character with C#

If you only have one word in the string, you can use TextInfo.ToTitleCase. No need to use Linq.

As @Guffa noted:

This will convert any string to title case, so, "hello world" and "HELLO WORLD" would both be converted to "Hello World".


To achieve exectly what you asked (convert all characters to lower, except the first one), you can do the following:

string mostLower = myString.Substring(0, 1) + myString.Substring(1).ToLower();

This can be done with simple string operations:

s = s.Substring(0, 1) + s.Substring(1).ToLower();

Note that this does exactly what you asked for, i.e. it converts all characters to lower case except the first one that is left unchanged.

If you instead also want to change the first character to upper case, you would do:

s = s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();

Note that this code assumes that there is at least two characters in the strings. If there is a possibility that it's shorter, you should of course test for that first.


String newString = new String(str.Select((ch, index) => (index == 0) ? ch : Char.ToLower(ch)).ToArray());

Tags:

C#