Coder Perfect

Changing the case of a string

Problem

I have a string that contains words in both upper and lower case letters.

Consider the following scenario: “a Simple string,” string myData = “a Simple string,” string myData = “a Simple string,” string myData =

Each word’s first character (separated by spaces) must be converted to upper case. As a result, I’d like the following outcome: var myData = “A Simple String”; var myData = “A Simple String”; var myData =

Is there a simple way to accomplish this? I’d like not split the string and convert it (that will be my last resort). It’s also a given that the strings will be in English.

Asked by Naveen

Solution #1

MSDN : TextInfo.ToTitleCase

Make sure that you include: using System. Globalization

string title = "war and peace";

TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;

title = textInfo.ToTitleCase(title); 
Console.WriteLine(title) ; //War And Peace

//When text is ALL UPPERCASE...
title = "WAR AND PEACE" ;

title = textInfo.ToTitleCase(title); 
Console.WriteLine(title) ; //WAR AND PEACE

//You need to call ToLower to make it work
title = textInfo.ToTitleCase(title.ToLower()); 
Console.WriteLine(title) ; //War And Peace

Answered by Kobi

Solution #2

Try this:

string myText = "a Simple string";

string asTitleCase =
    System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.
    ToTitleCase(myText.ToLower());

As previously stated, utilizing TextInfo.ToTitleCase may not get the exact results you desire. You could do something like this if you need greater control over the output:

IEnumerable<char> CharsToTitleCase(string s)
{
    bool newWord = true;
    foreach(char c in s)
    {
        if(newWord) { yield return Char.ToUpper(c); newWord = false; }
        else yield return Char.ToLower(c);
        if(c==' ') newWord = true;
    }
}

Then put it to use as follows:

var asTitleCase = new string( CharsToTitleCase(myText).ToArray() );

Answered by Winston Smith

Solution #3

Another variant on the theme. I’ve reduced it to this extension approach, which works wonderfully for my purposes, based on various ideas here:

public static string ToTitleCase(this string s) =>
    CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLower());

Answered by Todd Menier

Solution #4

I tried the TextInfo.ToTitleCase method myself, but I’m not sure why it doesn’t work when all the characters are upper-cased.

Though I prefer Winston Smith’s util function, I’ll offer the function I’m currently using:

public static String TitleCaseString(String s)
{
    if (s == null) return s;

    String[] words = s.Split(' ');
    for (int i = 0; i < words.Length; i++)
    {
        if (words[i].Length == 0) continue;

        Char firstChar = Char.ToUpper(words[i][0]); 
        String rest = "";
        if (words[i].Length > 1)
        {
            rest = words[i].Substring(1).ToLower();
        }
        words[i] = firstChar + rest;
    }
    return String.Join(" ", words);
}

Having some fun with some test strings:

String ts1 = "Converting string to title case in C#";
String ts2 = "C";
String ts3 = "";
String ts4 = "   ";
String ts5 = null;

Console.Out.WriteLine(String.Format("|{0}|", TitleCaseString(ts1)));
Console.Out.WriteLine(String.Format("|{0}|", TitleCaseString(ts2)));
Console.Out.WriteLine(String.Format("|{0}|", TitleCaseString(ts3)));
Console.Out.WriteLine(String.Format("|{0}|", TitleCaseString(ts4)));
Console.Out.WriteLine(String.Format("|{0}|", TitleCaseString(ts5)));

Giving output:

|Converting String To Title Case In C#|
|C|
||
|   |
||

Answered by Luis Quijada

Solution #5

I recently discovered a better option.

If your text contains every letter in uppercase, then TextInfo will not convert it to the proper case. We can fix that by using the lowercase function inside like this:

public static string ConvertTo_ProperCase(string text)
{
    TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
    return myTI.ToTitleCase(text.ToLower());
}

Now everything that comes in will be converted to Propercase.

Answered by Binod

Post is based on https://stackoverflow.com/questions/1206019/converting-string-to-title-case