public static class StringExtensions { public static string ToProperCase(this string text) { string result = ""; char apos = (char)39; for (int i = 0; i < text.Length; i++) { string s = text[i].ToString(); if (i == 0 || (!char.IsLetter(text[i - 1]) & text[i - 1] != apos)) result += s.ToUpper(); else result += s; } return result; } /// <summary> /// Takes a Proper-Cased variable name and adds a space before each /// capital letter. /// </summary> public static string AddSpaces(this string text) { // Special case: if input is all UPPERCASE, add no spaces if (text == text.ToUpperInvariant()) return text; var result = string.Empty; for (int i = 0; i < text.Length; i++) { // If we have multiple upper-case in a row, insert a space only before the last one if (i > 0 && char.IsUpper(text[i]) && (i == text.Length - 1 || char.IsLower(text[i - 1]) || char.IsLower(text[i + 1]))) result += " "; result += text.Substring(i, 1); } return result; } }