Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

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 smile_nerd. I wonder how many of us have written methods to do this...

Comments

Web Links 11.21.2006 « Rhonda Tipton’s WebLog said:

PingBack from http://rtipton.wordpress.com/2006/11/21/web-links-11212006/
# November 21, 2006 5:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)