21

How do convert a string to lowercase except for the first character? Can this be completed with LINQ?

Thanks

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
Ricky
  • 34,377
  • 39
  • 91
  • 131

8 Answers8

38

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();
Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 7
    That doesn't do what the OP asked for. Instead of turning the string `"THIS AND THAT"` into `"This and that"` it will turn it into `"This And That"`. – Guffa Dec 13 '10 at 09:51
  • 3
    Addendum: I just noticed that `ToTitleCase` leaves an all-uppercase string unchanged. It will turn `"hello world"` into `"Hello World"`, but it will leave `"HELLO WORLD"` unchanged. – Guffa Dec 15 '10 at 12:59
  • I second what Guffa said; the description here says so: https://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx – Kai Hartmann Aug 18 '15 at 12:42
19

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
18
String newString = new String(str.Select((ch, index) => (index == 0) ? ch : Char.ToLower(ch)).ToArray());
decyclone
  • 30,394
  • 6
  • 63
  • 80
  • 2
    Nice, interesting answer, but I don't think LINQ is the right tool for such low level ops. – ProfK Dec 13 '10 at 09:45
  • 2
    Agreed. I upvoted Oded's answer, too. But this is something I wanted to try using LINQ as OP said. – decyclone Dec 13 '10 at 09:47
7

Use namespace: using System.Globalization;

...

string value = CultureInfo.CurrentCulture.TextInfo.ToTitleCase("hello");

EDIT

This code work only if its single word .For convert all character into lower except first letter check Guffa Answer.

string value = myString.Substring(0, 1) + myString.Substring(1).ToLower();
Vyasdev Meledath
  • 8,926
  • 20
  • 48
  • 68
  • 1
    That doesn't do what the OP asked for. Instead of turning the string `"THIS AND THAT"` into `"This and that"` it will turn it into `"This And That"`. – Guffa Dec 13 '10 at 09:52
  • Addendum: I just noticed that `ToTitleCase` leaves an all-uppercase string unchanged. It will turn `"this and that"` into `"This And That"`, but it will leave `"THIS AND THAT"` unchanged. – Guffa Dec 15 '10 at 12:58
3

Not sure you can do it in linq here is a non-linq approach:

    public static string FirstCap(string value)
    {
        string result = String.Empty;

        if(!String.IsNullOrEmpty(value))
        {
            if(value.Length == 1)
            {
                result = value.ToUpper();
            }
            else
            {
                result = value.Substring(0,1).ToString().ToUpper() + value.Substring(1).ToLower();
            }
        }

        return result;
    }
Rob
  • 10,004
  • 5
  • 61
  • 91
3

based on guffa's example above (slightly amended). you could convert that to an extension method (please pardon the badly named method :)):

public static string UpperFirst(this string source)
{
    return source.ToLower().Remove(0, 1)
            .Insert(0, source.Substring(0, 1).ToUpper());
}

usage:

var myNewString = myOldString.UpperFirst();
// or simply referenced as myOldString.UpperFirst() where required

cheers guffa

jim tollan
  • 22,305
  • 4
  • 49
  • 63
2
var initialString = "Hello hOW r u?";          

var res = string.Concat(initialString..ToUpper().Substring(0, 1), initialString.ToLower().Substring(1));
  • priyanka, you will of course need to set the 1st part of that initialString ToUpper() :-) otherwise you'll just send out a lowercased string each time (unless the 1st letter was indeed a capital letter - as is your example). – jim tollan Dec 13 '10 at 11:10
0

You can use an extension method:

static class StringExtensions
{
    public static string ToLowerFirst(this string text)
        => !string.IsNullOrEmpty(text)
            ? $"{text.Substring(0, 1).ToLower()}{text.Substring(1)}"
            : text;
}

Unit tests as well (using FluentAssertions and Microsoft UnitTesting):

[TestClass]
public class StringExtensionsTests
{
    [TestMethod]
    public void ToLowerFirst_ShouldReturnCorrectValue()
        => "ABCD"
            .ToLowerFirst()
            .Should()
            .Be("aBCD");

    [TestMethod]
    public void ToLowerFirst_WhenStringIsEmpty_ShouldReturnCorrectValue()
        => string.Empty
            .ToLowerFirst()
            .Should()
            .Be(string.Empty);
}
Lidia
  • 131
  • 1
  • 5