Justin Walker

WEB DEVELOPER, CAT ENTHUSIAST, FAN OF VINTAGE COCKTAILS, TRAVEL, AND BREAKFAST BURRITOS

C# - String to Title Case

I was recently given a bunch of data to import where the names of businesses where all capitalized and I needed to store them in title case. TextInfo has a ToTitleCase method but it does not account for special characters (e.g. “O’BRIAN” or “BEST-COFFEE”).

I ended up writing a string extention method where I first run ToTitleCase then apply regex to look for letters following special characters. This also catches words with 's at the end so I run another regex replace to fix the possesive S.

I tried to combine the regex expressions to look for “letters after special characters except if they are 's at the end of a word.” I had no luck though and ended up two operations.

public static string ToTitleCase(this string s)
{
    // make the first letter of each word uppercase
    var titlecase = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s.ToLower());
    // match any letter after an apostrophe and make uppercase
    titlecase = Regex.Replace(titlecase, "[^A-Za-z0-9 ](?:.)", m => m.Value.ToUpper());
    // look for 'S at the end of a word and make lower
    titlecase = Regex.Replace(titlecase, @"('S)\b", m => m.Value.ToLower());
    return titlecase;
}

Load Comments