.ToTitleCase不工作的全部大写字符串字符串、全部、工作、ToTitleCase

2023-09-03 06:02:46 作者:明年再說

Public Function TitleCase(ByVal strIn As String)
      Dim result As String = ""
      Dim culture As New CultureInfo("en", False)
      Dim tInfo As TextInfo = culture.TextInfo()
      result = tInfo.ToTitleCase(strIn)
      Return result
 End Function

如果我输入TEST进入上述功能。输出是TEST。理想情况下输出测试

If I input "TEST" into the function above. The output is "TEST". Ideally it would output "Test"

我也尝试从这个职位无济于事code片段:使用ToTitleCase 的

I also tried the code snippets from this post to no avail: Use of ToTitleCase

推荐答案

如果没记错,ToTitleCase()似乎从来没有适​​用于所有的大写字符串。基本上,它要求你将字符串转换前处理为小写。

If memory serves, ToTitleCase() never seemed to work for all capitalized strings. It basically requires you to convert the string to lowercase prior to processing.

从MSDN:

一般地,标题套管将一个字的第一个字符   大写字母和字符的其余小写。然而,此   方法目前不提供适当的外壳来转换一个字   完全是大写,比如一个缩写。

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.

解决方法使用(在C#):​​

string yourString = "TEST";

TextInfo formatter = new CultureInfo("en-US", false).TextInfo;    
formatter.ToTitleCase(yourString.ToLower());