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.