比较字符串 比较什么?
Comparing Strings What is compared?
我有两个字符串:
String s1 ="11:7:34"
String s2="11:34:34"
当我比较这两个字符串时,我得到的结果是:false
(s1.compareTo(s2) < 0)
但是当我比较这个字符串时我得到 True:
String s1 ="11:07:34"
String s2="11:34:34"
所以我的问题是,在这种情况下会比较什么?字符串的长度或字符串的字符或什么?我认为“11:07:34”和“11:7:34”应该提供相同的结果!!
字符串的自然排序是字典顺序。两个String包含不同字符的第一个位置决定了比较的结果。
s1 11:7:34
s2 11:34:34 // the first non equal characters are 7 and 3, 7 comes after 3
_ // so s1 comes after s2 (i.e. s1.compareTo(s2) > 0)
s1 11:07:34
s2 11:34:34 // the first non equal characters are 0 and 3, 3 comes after 0
_ // so s1 comes before s2 (i.e. s1.compareTo(s2) < 0)
字符串是一个字符一个字符地比较的。由于 7
不小于 3
,但 0
是,您的结果是预期的。 H:m:s 的语义未被 String.compareTo
识别。
您可以阅读 documentation:
Compares two strings lexicographically. The comparison is based on the
Unicode value of each character in the strings. The character sequence
represented by this String object is compared lexicographically to the
character sequence represented by the argument string. The result is a
negative integer if this String object lexicographically precedes the
argument string. The result is a positive integer if this String
object lexicographically follows the argument string. The result is
zero if the strings are equal; compareTo returns 0 exactly when the
equals(Object) method would return true.
This is the definition of
lexicographic ordering. If two strings are different, then either they
have different characters at some index that is a valid index for both
strings, or their lengths are different, or both. If they have
different characters at one or more index positions, let k be the
smallest such index; then the string whose character at position k has
the smaller value, as determined by using the < operator,
lexicographically precedes the other string. In this case, compareTo
returns the difference of the two character values at position k in
the two string -- that is, the value:
this.charAt(k)-anotherString.charAt(k)
If there is no index position at which they differ, then the shorter
string lexicographically precedes the longer string. In this case,
compareTo returns the difference of the lengths of the strings -- that
is, the value:
this.length()-anotherString.length()
它比较字母/数字顺序。
所以顺序是
“11:0”
“11:3”
“11:7”
您认为它应该给出相同的结果,因为您能够将字符串解释为时间。 Java 不知道字符串表示时间值。
正如其他人所说,字符串是逐字符比较的,因此 3
小于 7
。
您应该考虑将字符串转换为代表时间的 class。例如使用新的 Java 8 日期和时间 API:
LocalTime t1 = LocalTime.parse(s1);
LocalTime t2 = LocalTime.parse(s2);
然后您可以使用
比较新值
t1.compareTo(t2) < 0
它将逐个字符与 ASCII 码进行比较。
顺便说一下,以这种方式比较时间不是一个好主意
我有两个字符串:
String s1 ="11:7:34"
String s2="11:34:34"
当我比较这两个字符串时,我得到的结果是:false
(s1.compareTo(s2) < 0)
但是当我比较这个字符串时我得到 True:
String s1 ="11:07:34"
String s2="11:34:34"
所以我的问题是,在这种情况下会比较什么?字符串的长度或字符串的字符或什么?我认为“11:07:34”和“11:7:34”应该提供相同的结果!!
字符串的自然排序是字典顺序。两个String包含不同字符的第一个位置决定了比较的结果。
s1 11:7:34
s2 11:34:34 // the first non equal characters are 7 and 3, 7 comes after 3
_ // so s1 comes after s2 (i.e. s1.compareTo(s2) > 0)
s1 11:07:34
s2 11:34:34 // the first non equal characters are 0 and 3, 3 comes after 0
_ // so s1 comes before s2 (i.e. s1.compareTo(s2) < 0)
字符串是一个字符一个字符地比较的。由于 7
不小于 3
,但 0
是,您的结果是预期的。 H:m:s 的语义未被 String.compareTo
识别。
您可以阅读 documentation:
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
this.charAt(k)-anotherString.charAt(k)
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
this.length()-anotherString.length()
它比较字母/数字顺序。
所以顺序是
“11:0” “11:3” “11:7”
您认为它应该给出相同的结果,因为您能够将字符串解释为时间。 Java 不知道字符串表示时间值。
正如其他人所说,字符串是逐字符比较的,因此 3
小于 7
。
您应该考虑将字符串转换为代表时间的 class。例如使用新的 Java 8 日期和时间 API:
LocalTime t1 = LocalTime.parse(s1);
LocalTime t2 = LocalTime.parse(s2);
然后您可以使用
比较新值t1.compareTo(t2) < 0
它将逐个字符与 ASCII 码进行比较。 顺便说一下,以这种方式比较时间不是一个好主意