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

shawn said:

Be aware that Title casing isn't correct linguistically, particularly for non-English languages.  (In English we don't title case words like "of" and "a").

# July 9, 2009 11:02 PM

Fadzai Chamba said:

Funny I came across this when I was about to do this as my first blog here.

I often find myself writing code to bridge the little differences between VB (my preferred langauge) and C# for other developers so that we can all be on the same page.

VB has always had the StrConv() method/function which accepts as a parameter the type of conversion you want to make.

With the advent of .NET 3.5 and extension methods, I have written this into a class library for my C# based colleagues so that we can do the same kind of processing for certain fields in our applications.

We always Title Case (Proper Case in VB) customer names and job titles but the C# guys had no way of doing this. I then wrote a little utility class for them with extension  methods that look like so..

//This is air-code so excuse any errors

   public static class StringExtensions

   {

       public static string ToTitle(this string input)

       {

           return input.ToTitle(System.Globalization.CultureInfo.CurrentCulture);

       }

       public static string ToTitle(this string input, System.Globalization.CultureInfo culture)

       {

           return culture.TextInfo.ToTitleCase(input);

       }

       public static string ToTitleInvariant(this string input)

       {

           return input.ToTitle(System.Globalization.CultureInfo.InvariantCulture);

       }

   }

You would then call this like a method on the string class and it gets everyone on the same page.

# July 14, 2009 2:42 PM

schlub said:

Just what I was looking for. I have clients that like to input titles in uppercase, but now I can force it to appear as title case like it should ;)

# August 10, 2010 11:25 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 1 and 7 and type the answer here: