在 java 中使用 compareTo() 方法比较大写和小写
comparing uppercase and lowercase using compareTo() method in java
我想在 java 中使用 compareTo()
方法比较大小写。
下面是我使用过但无法理解为什么返回 32 的代码片段。
String s1="a";
String s2="A";
System.out.println(s1.compareTo(s2));
//return 32
s1.compareTo(s2)
会做:
'a' - 'A' => 97 - 65 = 32
来自 Java 文档:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
Java – String compareTo() Method example. The method compareTo() is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value.
所以`'a' - 'A' => 97 - 65 = 32
我想在 java 中使用 compareTo()
方法比较大小写。
下面是我使用过但无法理解为什么返回 32 的代码片段。
String s1="a";
String s2="A";
System.out.println(s1.compareTo(s2));
//return 32
s1.compareTo(s2)
会做:
'a' - 'A' => 97 - 65 = 32
来自 Java 文档:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
Java – String compareTo() Method example. The method compareTo() is used for comparing two strings lexicographically. Each character of both the strings is converted into a Unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value.
所以`'a' - 'A' => 97 - 65 = 32