Java Reference
In-Depth Information
Checking the Start and End of a String
It can be useful to be able to check just part of a string. You can test whether a string starts with a
particular character sequence by using the method
startsWith()
. If
string1
has been defined as
"
Too
many
cooks
", the expression
string1.startsWith("Too")
will have the value
true
. So
would the expression
string1.startsWith("Too man")
. The comparison is case sensitive so the
expression
string1.startsWith("tOO")
will be
false
.
A complementary method
endsWith()
checks for what appears at the end of a string, so the expression
string1.endsWith("cooks")
will have the value
true
. The test is case sensitive here, too.
Sequencing Strings
You will often need to place strings in order, for example, when you have a collection of names. Testing
for equality doesn't help - what you need is the method
compareTo()
in the class
String
. This
method compares the
String
object from which it is called with the argument passed to it, and returns
an integer which is negative if the
String
object is less than the argument passed, zero if the S
tring
object is equal to the argument, and positive if the
String
object is greater than the argument. It is not
that obvious what the terms 'less than', 'equal to', and 'greater than' mean when applied to strings, so
let's define that a bit more precisely.
Two strings are compared in the
compareTo()
method by comparing successive corresponding
characters, starting with the first character in each string. The process continues until a pair of
corresponding characters are found to be different, or the last character in the shortest string is reached.
Individual characters are compared by comparing 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 representation is greater than that of the other.
One string is greater than another if it has a character greater than the corresponding character in the
other string, and all the previous characters were equal. So if
string1
has the value "
mad dog
", and
string2
has the value "
mad
cat
", then the expression:
string1.compareTo(string2)
will return 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
"
catamaran
" 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 will return 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.
We can exercise the
compareTo()
method in a simple example.