开发者

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 documentatio开发者_如何学运维n, any insights?


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.


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?


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"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜