How to: Change casing in Text to TitleCase
At Tech-Ed 2006 I attended a session about hidden treasures in .Net. One of the treasures mentioned there was the ToTitleCase() method. This method changes the case of the first character of each word in a text to Upper case:
System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase();
The method works fine, but when something is already in upper case it is not changed. Also, having to type the full path to the current culture in the current thread is a bit annoying. So I created the following method which allows me to use this function a bit simpler:
/// <summary>
/// Change the case of the first letter of each word to upper case.
/// </summary>
/// <param name="text">The string to convert to title case.</param>
/// <param name="culture">The culture information to be used.</param>
/// <param name="forceCasing">When true, forces all words to be lower case before changing everything to title case.</param>
/// <returns>The string in title case.</returns>
private string ToTitleCase(string text, System.Globalization.CultureInfo culture, bool forceCasing)
{ if (forceCasing)
{ return culture.TextInfo.ToTitleCase(text.ToLower());
}
return culture.TextInfo.ToTitleCase(text);
}
I also implemented a few overloaded methods that use the one above:
/// <summary>
/// Change the case of the first letter of each word to upper case.
/// </summary>
/// <param name="text">The string to convert to title case.</param>
/// <returns>The string in title case.</returns>
private string ToTitleCase(string text)
{ return ToTitleCase(text, System.Threading.Thread.CurrentThread.CurrentCulture, false);
}
/// <summary>
/// Change the case of the first letter of each word to upper case.
/// </summary>
/// <param name="text">The string to convert to title case.</param>
/// <param name="forceCasing">When true, forces all words to be lower case before changing everything to title case.</param>
/// <returns>The string in title case.</returns>
private string ToTitleCase(string text, bool forceCasing)
{ return ToTitleCase(text, System.Threading.Thread.CurrentThread.CurrentCulture, forceCasing);
}
/// <summary>
/// Change the case of the first letter of each word to upper case.
/// </summary>
/// <param name="text">The string to convert to title case.</param>
/// <param name="culture">The culture information to be used.</param>
/// <returns>The string in title case.</returns>
private string ToTitleCase(string text, System.Globalization.CultureInfo culture)
{ return ToTitleCase(text, culture, false);
}
The following code can be used to test the functionality:
private void TestTitleCase()
{ System.Globalization.CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentCulture;
string text1 = "change this to title case.";
string text2 = "chanGe This to TITLE case.";
string text3 = "ChanGe This to TITLE case.";
Console.WriteLine(string.Format(culture, "[{0}] => [{1}]", text1, ToTitleCase(text1))); Console.WriteLine(string.Format(culture, "[{0}] => [{1}]", text2, ToTitleCase(text2))); Console.WriteLine(string.Format(culture, "[{0}] => [{1}]", text3, ToTitleCase(text3))); Console.WriteLine(string.Format(culture, "[{0}] => [{1}]", text3, ToTitleCase(text3, true)));}
The code produces the following result:
[change this to title case.] => [Change This To Title Case.]
[chanGe This to TITLE case.] => [Change This To TITLE Case.]
[ChanGe This to TITLE case.] => [Change This To TITLE Case.]
[ChanGe This to TITLE case.] => [Change This To Title Case.]
You will notice the effect that casing has on this method. When a word still contains lower case, then the word is changed to Title case. But when the word is already in upper case, then the method leaves the casing as it is. Which explains why I added a forceCasing boolean as an argument.
Is it useful? Probably not. But I still found it interesting enough to blog about
. I wonder how many of us have written methods to do this...