Converting a string to proper case in .NET

In my current project, I'm pulling data from a table that originates on a mainframe.  Thus, the text is mostly ALL CAPS.  Yuck. 

My first thought for converting the data to proper case was to write a bunch of Substring calls.  This seemed like a hack.  My next thought was to use the VB strconv function, but since I'm writing this app in C#, I didn't want to commit the ultimate sin of including the Visual Basic namespace.

A quick google search came up TextInfo.ToTitleCase().  This looked promising, so I knocked together this code:

using System.Globalization;
public static string ToProperCase(string data) {
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(data);
}

Looks like it should work, right?  I modified my app to use this new function, but lo and behold, the data was still ALL CAPS when it was displayed.  WTF?  I stepped through the code; I tried using the Command window; I surfed some more...nothing worked.

I finally snagged the code from this KB article.  It worked. :-\  My frustration level was growing...I changed the “title” variable in the sample code so it was ALL CAPS, recompiled and ran the test app.  It DIDN'T WORK!  Aha...has something to do with ALL CAPS.

I ended up modifying my ToProperCase method:
using System.Globalization;
public static string ToProperCase(string data) {
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(data.ToLower());
}

This is my final, working solution.  Seems bogus to me.  Maybe one of these days I'll dive deeper into the issue.

Print | posted on Thursday, May 13, 2004 9:25 AM

Feedback

# re: Converting a string to proper case in .NET

left by Banu at 1/4/2006 11:41 PM Gravatar
Its simply great!!

# re: Converting a string to proper case in .NET

left by john couture at 8/29/2008 10:44 PM Gravatar
In case you are still interested, there is a function called strConv that has several parameters ... one of them being propercase and it works quite well.

http://msdn.microsoft.com/en-us/library/cd7w43ec(VS.80).aspx

# re: Converting a string to proper case in .NET

left by Michael at 8/30/2008 1:13 PM Gravatar
StrConv requires a reference to Microsoft.VisualBasic.

While that in and of itself isn't a big deal, some people don't like that idea. :-)
Title  
Name
Email (never displayed)
Url
Comments   
Please add 8 and 3 and type the answer here: