by Dinesh Thakur

The String class also provides a number of methods for comparing strings. While discussing these, we assume string sl contains 'Welcome' in it.

boolean equals (String str): This method checks whether the contents of the String object referenced by str is equal to the contents of String object through which this methodis invoked. It returns true if the strings are equal and false otherwise. For example:

sl.equals("Welcome") // returns true

sl.equals(s2) // returns false

 

boolean equalslgnoreCase (String str) : It is same as above except it ignores the case of the characters in the string (i.e treats uppercase and lowercase characters to be equivalent). For example,

sl.equalslgnoreCase("welcome") // returns true

sl.equalslgnoreCase(s2) // returns true

int compareTo (String str) : This method compares the contents of String object referenced by str with the contents of invoking string by comparing the successive corresponding characters, starting with the first character in each string. The process continues until either the corresponding characters are found to be different or the last character in one or both strings is reached. Characters are compared lexicographically (i.e. according to their unicode representation). This method returns

if the two strings are equal i.e. they contain the same number of characters and corresponding characters are identical.

a value greater than 0 if the invoking string has a character greater than the corresponding character in the String object referenced by str.

a value less than 0 if the invoking string has a character less than the corresponding character in the String object referenced by str. For example:

sl.compareTo(“Welcome") // returns 0

sl.compareTo(s2) // returns -32

s2.compareTo{sl) // returns 32

NOTE: A syntax error will occur if you compare strings using comparison operators >, >=, <=, < .

int compareTolgnoreCase (String str) : This method is same as compareTo () method except that it ignores the case of string characters. For example: sl. compareTolgnoreCase (s2) returns 0.

boolean startsWi th (Sting str) : This method determines whether the invoking string starts with the specified string str. If so, the method returns true otherwise returns false. For example,

sl.startsWith(“We") //returns true

sl.startsWith(“Well") //returns false

boolean startsWith(string str, int startlndex): It is similar to the previous method but it tests whether the invoking string starts with the specified string str beginning at a specified index, startlndex. For example,

sl.startsWith(“We",2) // returns false

boolean endsWith (String str) : This method determines whether the invoking string ends with the specified string str or not. If so the method returns true otherwise returns false. For example:

s3.endswith(“Java") // returns true

s1. endswith (“come") // returns false

boolean regionMatches(int startlndex, String str, int strStartlndex, int len) : This method checks whether a region (portion) of the invoking string matches the specified region of the specified String object referenced by str. The parameter startIndex specifies the starting index in the string that invokes the method. The parameter str is the comparison string and the parameter strStartlndex specifies the starting index in the string str. The last parameter len specifies the number of characters to compare between two strings. It returns true if the specified number of characters are lexicographically equivalent. For example,

sl.regionMatches(2,"come",1,3) // returns true

boolean regionMatches(boolean ignoreCase, int startlndex, String str, int strStartlndex, int len) :It is similar to the above method except that it will ignorecase if the value in the first parameter ignoreCal3e is true. For example,

sl.regionMatches(true,2,"COME",1,3) // returns true.

Now let us consider a example to shows how these work.

public class StringCompareMethods

{
           public static void main(String[] args)
      {
            String str = "welcome" ;
            System.out.println("str.equals(\"welcome\") ="+str.equals("Welcome"));
            System.out.println("str.equalsIgnoreCase(\"welcome\") = "+ str.equalsIgnoreCase("welcome"));
            System.out.println("str.compareTo(\"welcome\") = "+ str.compareTo("welcome"));
            System.out.println("str.compareToIgnoreCase(\"welcome\") = "+ str.compareToIgnoreCase("welcome"));
            System.out.println("str.startsWith(\"we\") = "+ str.startsWith("We"));
            System.out.println("str,startsWith(\"We\",2) = "+ str.startsWith("We",2));
            System.out.println(" str.endsWith(\"me\") = "+ str.endsWith("me"));
            System.out.println("str.regionMatches(4,\"COME\",1,3) = " + str.regionMatches(4, "COME",1,3));
            System.out.println("str.regionMatches(true,4,\"come\",1,3) = " + str.regionMatches(true,4,"come",1,3));
      }
}

String Compare Methods in Java