Scott Muc

Another .Net Developer Named Scott

  Home  |   Contact  |   Syndication    |   Login
  29 Posts | 0 Stories | 27 Comments | 4 Trackbacks

News

Archives

Post Categories

Blogging Tools

Personal

Work Related

Strange how the string class almost has everything you need. I came across the need to Title Case some text, because I couldn't guarantee the text that I receive is properly formatted. I automatically assumed I could simply use .ToTitleCase() on my string. Woops, compilation error!

Luckily a search found a quick and easy solution, and I wrote the beginnings of my StringHelper class. It's first method? The .ToTitleCase() method.

public static class StringHelper
{
    public static string ToTitleCase(string text)
    {
        return StringHelper.ToTitleCase(text, CultureInfo.InvariantCulture);
    }
    
    public static string ToTitleCase(string text, CultureInfo cultureInfo)
    {
        if (string.IsNullOrEmpty(text)) return string.Empty;
    
        TextInfo textInfo = cultureInfo.TextInfo;
        return textInfo.ToTitleCase(text.ToLower());
    }
}

Some credit goes to this post for letting me know the all upper case issue.

Yeah, I know this has been discussed eons ago. This is mostly for my own reference.

posted on Monday, March 26, 2007 12:58 PM

Feedback

# re: string.ToTitleCase() 4/3/2007 10:34 AM Anonymous
WTF? Why would you name your method ToUpperCase if you're returning the TitleCase of the string? How does that make sence? This should be posted on the DailyWTF!!! HAHA

# re: string.ToTitleCase() 4/3/2007 10:49 AM Scott Muc
That is what people call a Copy+Paste Exception. Took a while to catch, but it's resolved now :)

# re: string.ToTitleCase() 4/19/2007 5:20 AM Daniel Moth
1. You still have references to UpperCase in your blog post
2. You forgot to include the return types in your two static methods: string
3. Since StringHelper only has static methods you should also make it static.

After you have catered for the above, the client code looks like this:
StringHelper.ToTitleCase(someString);

Not only that is longer than necessary, but it is also not discoverable by intellisense (in other words, the developer must *know* there is a StringHelper utlity class somewhere). You can fix this in C#3 using Extension Methods (see my blog).

After simply adding the keyword this to your helper method i.e.
public static string ToTitleCase(this string text) {...}

you can now call the code like this:
someString.ToTitleCase();

:-)

# re: string.ToTitleCase() 4/19/2007 9:55 AM Scott Muc
Bah... I need to improve my reviewing of my content. It's so easy to skim over it and believe it looks correct.

Extension Methods look awesome! I've always seen cases where I want to inherit everything from a base class (like string) and add a couple methods to it, but I don't want to change all my string declarations to the new class I've defined. It seems Extension Methods resolve that issue.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: