Nimesh Madhavan
Nimesh Madhavan

Reputation: 6318

TextInfo.ToTitleCase does not work as expected for ALL CAPS strings

I was trying to use TextInfo.ToTitleCase to convert some names to proper case. it works fine for strings in lowercase and mixed case but for strings with all characters in upper case, it returns the input string as is.
Nothing about this behavior is mentioned in MSDN documentation, any insights?

Upvotes: 50

Views: 19542

Answers (3)

Kangkan
Kangkan

Reputation: 15571

The MSDN documentation says:

Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. However, a word that is entirely uppercase, such as an acronym, is not converted.

So it works as intended. Try TextInfo.ToTitleCase(TextInfo.ToLowerCase("STRINGINCAPS"))

like:

string TitleCaseString;
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
TitleCaseString = myTI.ToTitleCase(myTI.ToLowerCase("STRINGINCAPS"));

Upvotes: 7

Mikael Svenson
Mikael Svenson

Reputation: 39695

From MSDN docs:

Remarks Generally, title casing converts the first character of a word to uppercase and the rest of the characters to lowercase. However, this method does not currently provide proper casing to convert a word that is entirely uppercase, such as an acronym. The following table shows the way the method renders several strings.

so it's expected behaviour. You could lowercase your string first if it's all uppercase, then run ToTitleCase on it.

Upvotes: 93

Jon Skeet
Jon Skeet

Reputation: 1500815

I suspect it's because words in all capitals are expected to be abbreviations such as USA.

For example, you wouldn't expect "Earthquake hits USA" to be changed to "Earthquake Hits Usa" would you?

Upvotes: 11

Related Questions