String class in Java provides different methods for comparing strings or substrings within strings. In this tutorial I will discuss about those Java string comparison methods.
equals() and equalsIgnoreCase()
We can use equals() method to check equality of two strings. It has following syntax.
Syntax
boolean equals(String str)
Here str is the String object and equals() method is invoked by another String object. It returns true if both the strings are exactly same otherwise returns false. The comparison is case sensitive.
If we want to compare strings ignoring case sensitivity then we can use equalsIgnoreCase() method. It has following syntax.
Syntax
boolean equalsIgnoreCase(String str)
Example
An example is given below that shows how to use equals() and equalsIgnoreCase() methods in Java.
class temp { public static void main(String...s) { String str1="Hello"; String str2="HELLO"; System.out.println(str1+" equals "+str2+" -> "+str1.equals(str2)); System.out.println(str1+" equalsIgnoreCase "+str2+" -> "+str1.equalsIgnoreCase(str2)); } }
Output
Hello equals HELLO -> false
Hello equlsIgnoreCase HELLO -> true
regionMatches()
This method is used to compare a specific region of first string with the specific region of second string. regionMatches() has following syntax.
Syntax
boolean regionMatches(int str1StartIndex, String str2, int str2StartIndex, int numChars)
Here str1StartIndex is the index at which region begins in first string while str2StartIndex is the index at which region begins in second string. str2 is the second string and the method is invoked by first string. The length of the region that is to be compared is specified by numChars.
If we want to compare specific regions in two strings ignoring the case of characters then we use overloaded version of regionMatches() method. It has following syntax.
boolean regionMatches(boolean ignoreCase, int str1StartIndex, String str2, int str2StartIndex, int numChars)
If ignoreCase is true then case of characters not considered and if false then case of characters is considered
Example
Let us take one example to understand this method.
class temp { public static void main(String...s) { String str1="Hello World"; String str2="HELLO WORLD"; System.out.println("Normal Version -> "+str1.regionMatches(0,str2,0,5)); System.out.println("Overloaded Version -> "+str1.regionMatches(true,0,str2,0,5)); } }
Output
Normal Version -> false
Overloaded Version -> true
startsWith() and endsWith()
startsWith() method checks if a given string begins with the specified string and endsWith() method checks if a given string ends with the specified string. These two methods are the specialized form of regionMatches(). They have following syntax.
Syntax
boolean statrsWith(String str) boolean endsWith(String str)
Example
class temp { public static void main(String...s) { String str="BarBar"; System.out.println(str.startsWith("Bar")); System.out.println(str.endsWith("Bar")); } }
Outptut
true
true
compareTo() and compareToIgnoreCae()
This method checks whether a string is less than, greater than or equal to another string. compareTo() is mainly used when we want to sort so many string in dictionary order. It has following syntax.
Syntax
int compareTo(String str)
Less than zero – The invoking string is less than str.
Greater than zero – The invoking string is greater than str.
Zero – The two strings are equal.
Example
class temp { public static void main(String...s) { String str1="Bar", str2="Bat",str3="Bag",str4="Bar"; System.out.println(str1+" and "+str2+" -> "+str1.compareTo(str2)); System.out.println(str1+" and "+str3+" -> "+str1.compareTo(str3)); System.out.println(str1+" and "+str4+" -> "+str1.compareTo(str4)); } }
Output
Bar and Bat -> -2
Bar and Bag -> 11
Bar and Bar -> 0
The value that compareTo() returns is the difference between the ASCII values of first unequal characters in both strings. In above example when we compared “Bar” and “Bat” then -2 is returned. Because in both strings the first unequal character is ‘r’ and ‘t’, there ASCII values difference is -2.
The compareTo() method consider case difference while comparing the strings. If you want to ignore case difference you can use compareToIgnoreCase() method. It has following syntax.
int CompareToIgnoreCase(String str)
This was the detailed tutorial about Java string comparison. If you have any doubts regarding above tutorial then feel free to ask by commenting below.