我想使用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 - java中使用compareTo()方法比较大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50166930/