Java String Comparison

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

 

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

 

Example

An example is given below that shows how to use equals() and equalsIgnoreCase() methods in Java.

 

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

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.

 

 

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.

 

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

 

Example

 

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

 

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

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.

 

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.

Leave a Reply

Your email address will not be published. Required fields are marked *