gpt4 book ai didi

java - Character.toUpperCase() 和 Character.toTitleCase() 有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:59:44 26 4
gpt4 key购买 nike

我正在重构我的一些旧代码,然后我发现我在某个时候使用了 Character.toTitleCase() 方法并且不禁想知道 Character. toUpperCase() 会更好。

我阅读了他们的描述,没有发现任何基本区别:

toUpperCase

Converts the character argument to uppercase using case mapping information from the UnicodeData file. Note that Character.isUpperCase(Character.toUpperCase(ch)) does not always return true for some ranges of characters, particularly those that are symbols or ideographs.

In general, String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case mapping methods cannot.

Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the toUpperCase(int) method.

toTitleCase

Converts the character argument to titlecase using case mapping information from the UnicodeData file. If a character has no explicit titlecase mapping and is not itself a titlecase char according to UnicodeData, then the uppercase mapping is returned as an equivalent titlecase mapping. If the char argument is already a titlecase char, the same char value will be returned. Note that Character.isTitleCase(Character.toTitleCase(ch)) does not always return true for some ranges of characters.

Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the toTitleCase(int) method.

然后我试着像这样测试它们:

public class Test {
public static void main(String... args) {

String originalString = "abcdefghijklmnopqrstuvwxyz123546-.,/*&%+";
StringBuilder upperCaseStringBuilder = new StringBuilder();
StringBuilder titleCaseStringBuilder = new StringBuilder();

for (int i = 0; i < originalString.length(); i++) {
upperCaseStringBuilder.append(Character.toUpperCase(originalString.charAt(i)));
titleCaseStringBuilder.append(Character.toTitleCase(originalString.charAt(i)));
}

System.out.println("Original String : " + originalString);
System.out.println("UpperCase result: " + upperCaseStringBuilder.toString());
System.out.println("TitleCase result: " + titleCaseStringBuilder.toString());
}
}

这是输出:

Original String : abcdefghijklmnopqrstuvwxyz123546-.,/*&%+
UpperCase result: ABCDEFGHIJKLMNOPQRSTUVWXYZ123546-.,/*&%+
TitleCase result: ABCDEFGHIJKLMNOPQRSTUVWXYZ123546-.,/*&%+

所以我无法理解这两种方法之间的区别。正如我之前所说,我在代码中使用了 toTitleCase() 来将 String 大写。

是否有任何我没有考虑到的关键差异,这些差异可能会导致我的代码在某些特殊情况下表现出与预期不同的行为?


注意:我不认为这与 String capitalize - better way 重复.因为在那个问题中,问题在于字符串大写的性能,而不是在这个问题中字符的大写和标题大小写。

最佳答案

标准的 ASCII 字符太无聊了!这是东西more exciting :

System.out.println(Character.toTitleCase('dz'));  // Dz
System.out.println(Character.toUpperCase('dz')); // DZ

Live demo.

关于java - Character.toUpperCase() 和 Character.toTitleCase() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47887244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com