Candor.StringExtensions.ToProperNameCase C# (CSharp) Method

ToProperNameCase() public static method

Converts a string to a format of a proper name. Each word is capitalized. underscores become spaces (word boundaries). Acronyms are assumed when the text is all letters in all caps, and then no change is made.
public static ToProperNameCase ( this text ) : string
text this
return string
        public static string ToProperNameCase(this string text)
        {
            if (string.IsNullOrEmpty(text))
                return String.Empty;
            text = text.Replace("_", " ");
            var newText = new StringBuilder(text.Length * 2);
            var prevTriggerCaseUpper = true;
            var allLetters = true;
            foreach (var curr in text)
            {
                if (prevTriggerCaseUpper && char.IsLetter(curr))
                    newText.Append(char.ToUpper(curr));
                else
                    newText.Append(char.ToLower(curr));

                allLetters = (allLetters && char.IsLetter(curr));
                prevTriggerCaseUpper = (char.IsWhiteSpace(curr) || curr == '-' || curr == '_' || (char.IsNumber(curr) && prevTriggerCaseUpper));
            }

            if (allLetters && text.ToUpper() == text)
                return text; //acronym

            return newText.ToString();
        }