Java Reference
In-Depth Information
How do you compare the contents of two strings? You might attempt to use the == operator,
as follows:
if (string1 == string2)
System.out.println( "string1 and string2 are the same object" );
else
System.out.println( "string1 and string2 are different objects" );
==
However, the == operator checks only whether string1 and string2 refer to the same
object; it does not tell you whether they have the same contents. Therefore, you cannot use the
== operator to find out whether two string variables have the same contents. Instead, you should
use the equals method. The following code, for instance, can be used to compare two strings:
if (string1.equals(string2))
System.out.println( "string1 and string2 have the same contents" );
else
System.out.println( "string1 and string2 are not equal" );
string1.equals(string2)
For example, the following statements display true and then false .
String s1 = "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 lexico-
graphically (i.e., in terms of Unicode ordering) less than s2 , and a value greater than 0 if s1
is lexicographically 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 relational 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 and compareToIgnore-
Case methods for comparing strings. The equalsIgnoreCase and compareToIgnore-
Case methods ignore the case of the letters when comparing two strings. You can also
use str.startsWith(prefix) to check whether string str starts with a specified prefix,
str.endsWith(suffix) to check whether string str ends with a specified suffix, and str
.contains(s1) to check whether string str contains string s1 . For example,
"Welcome to Java".startsWith("We") returns true .
"Welcome to Java".startsWith("we") returns false .
"Welcome to Java".endsWith("va") returns true .
 
 
Search WWH ::




Custom Search