C# and Capitalizing string

The class string in C# has methods for ToLower(), ToUpper(), but not for ToCapitalize() og something like that. When I started to learn C# some years back that was something that surprised me alot – and it still do. The method for capitalizing strings, is called ToTitleCase and is located in the TextInfo class. The best way to capitalize string id to instansiate a CultureInfo object, retrieve the TextInfo instans and call the ToTitleCase method with the string.

        public static string ToInitialCase(string str)
            var textInfo = new CultureInfo("nb-NO", false).TextInfo;
            string tmp = textInfo.ToTitleCase(str.ToLower());
            return tmp;

Be aware of the ToLower call. I’m not sure if this is a bug, but the capitalizing didn’t correctly my norwegian letters before I had the ToLower() call.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s