java - java中使用compareTo()方法比较大小写

标签 java string compareto

我想使用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/

相关文章:

java - 将 Hibernate、JDBC 和 SpringSecurity 连接在一起时出现问题

c++ - 字符串运算符+(重载)char数组和string.in cpp中的不同行为

java - 如何在同一行代码中正确使用instanceof和compareTo?

java - 为什么 CompareTo() 不起作用?

Java jList1.getSelectedValue() 返回一个对象。我需要所选列表项中的字符串。

java - 是否可以将 JCheckBox 放入不包含 boolean 值的 JTable 单元格中?

java - 如何使用多态性将对象映射到辅助类?

c++ - 中间函数

javascript - 如何通过用户定义的路径创建文本文件

java - 使用一个参数在java中创建compareTo方法