Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

vb.net

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?

like image 746
Nimesh Madhavan Avatar asked Apr 23 '10 08:04

Nimesh Madhavan


2 Answers

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.

like image 143
Mikael Svenson Avatar answered Sep 20 '22 02:09

Mikael Svenson


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?

like image 10
Jon Skeet Avatar answered Sep 19 '22 02:09

Jon Skeet