Java Reference
In-Depth Information
java.lang.String
+equals(s1: Object): boolean
+equalsIgnoreCase(s1: String):
boolean
+compareTo(s1: String): int
Returns true if this string is equal to string
s1
.
Returns true if this string is equal to string
s1
case
insensitive.
Returns an integer greater than
0
, equal to
0
, or less than
0
to indicate whether this string is greater than, equal to, or
less than
s1
.
Same as
compareTo
except that the comparison is case
insensitive.
Returns true if the specified subregion of this string exactly
matches the specified subregion in string
s1
.
Same as the preceding method except that you can specify
whether the match is case sensitive.
+compareToIgnoreCase(s1: String):
int
+regionMatches(index: int, s1: String,
s1Index: int, len: int): boolean
+regionMatches(ignoreCase: boolean,
index: int, s1: String, s1Index: int,
len: int): boolean
+startsWith(prefix: String): boolean
+endsWith(suffix: String): boolean
Returns true if this string starts with the specified prefix.
Returns true if this string ends with the specified suffix.
F
IGURE
9.2
The
String
class contains the methods for comparing strings.
Note that parameter type for the
equals
method is
Object
. We will introduce the
Object
class in Chapter 11. For now, you can replace
Object
by
String
for using the
equals
method
to compare two strings. For example, the following statements display
true
and then
false
.
String s1 =
new
String(
"Welcome to Java"
);
String s2 =
"Welcome to Java"
;
String s3 =
"Welcome to C++"
;
System.out.println(s1.equals(s2));
// true
System.out.println(s1.equals(s3));
// false
The
compareTo
method can also be used to compare two strings. For example, consider the
following code:
s1.compareTo(s2)
s1.compareTo(s2)
The method returns the value
0
if
s1
is equal to
s2
, a value less than
0
if
s1
is lexicographi-
cally (i.e., in terms of Unicode ordering) less than
s2
, and a value greater than
0
if
s1
is lexi-
cographically greater than
s2
.
The actual value returned from the
compareTo
method depends on the offset of the first
two distinct characters in
s1
and
s2
from left to right. For example, suppose
s1
is
abc
and
s2
is
abg
, and
s1.compareTo(s2)
returns
-4
. The first two characters (
a
vs.
a
) from
s1
and
s2
are compared. Because they are equal, the second two characters (
b
vs.
b
) are compared.
Because they are also equal, the third two characters (
c
vs.
g
) are compared. Since the char-
acter
c
is
4
less than
g
, the comparison returns
-4
.
Caution
Syntax errors will occur if you compare strings by using comparison operators
>
,
>=
,
<
, or
<=
. Instead, you have to use
s1.compareTo(s2)
.
Note
The
equals
method returns
true
if two strings are equal and
false
if they are not.
The
compareTo
method returns
0
, a positive integer, or a negative integer, depending
on whether one string is equal to, greater than, or less than the other string.
The
String
class also provides the
equalsIgnoreCase
,
compareToIgnoreCase
, and
regionMatches
methods for comparing strings. The
equalsIgnoreCase
and