Java Reference
In-Depth Information
}
The comparison is case-sensitive so the expression
string1.startsWith("tOO")
results in the value
false
.
A complementary method
endsWith()
checks for what appears at the end of a string, so the expression
string1.endsWith("cooks")
has the value
true
. The test is case-sensitive here, too.
Sequencing Strings
You'll often want to place strings in order — for example, when you have a collection of names. Testing for
equality doesn't help because to sort strings you need to be able to determine whether one string is greater
than or less than another. What you need is the
compareTo()
method in the
String
class. This method com-
pares the
String
object for which it is called with the
String
argument you pass to it and returns an integer
that is negative if the
String
object is less than the argument that you passed, zero if the S
tring
object
is equal to the argument, and positive if the
String
object is greater than the argument. Of course, sorting
strings requires a clear definition of what the terms
less than
,
equal to
, and
greater than
mean when applied
to strings, so I explain that first.
The
compareTo()
method compares two strings by comparing successive corresponding characters,
starting with the first character in each string. The process continues until either corresponding characters are
found to be different, or the last character in one or both strings is reached. Characters are compared by com-
paring their Unicode representations — so two characters are equal if the numeric values of their Unicode
representations are equal. One character is greater than another if the numerical value of its Unicode repres-
entation is greater than that of the other. A character is less than another if its Unicode code is less than that
of the other.
One string is greater than another if the first character that differs from the corresponding character in the
other string is greater than the corresponding character in the other string. So if
string1
has the value
"mad
dog"
, and
string2
has the value
"mad cat"
, then the expression
string1.compareTo(string2)
returns a positive value as a result of comparing the fifth characters in the strings: the
'd'
in
string1
with
the
'c'
in
string2
.
What if the corresponding characters in both strings are equal up to the end of the shorter string, but the
other string has more characters? In this case the longer string is greater than the shorter string, so
"cata-
maran"
is greater than
"cat"
.
One string is less than another string if it has a character less than the corresponding character in the other
string, and all the preceding characters are equal. Thus, the following expression returns a negative value:
string2.compareTo(string1)
Two strings are equal if they contain the same number of characters and corresponding characters are
identical. In this case the
compareTo()
method returns 0.
You can exercise the
compareTo()
method in a simple example.
TRY IT OUT: Ordering Strings