If you have to compare two Strings in Java or the portion of two Strings on the basis of the content of those Strings then you can do it using one of the following methods-
- equals() method or equalsIgnoreCase() if you don’t want to consider case. See example.
- compareTo() method or compareToIgnoreCase() if you don’t want to consider case. See example.
- For comparing portion of the String you can use startsWith() and endsWith() methods. See example.
- To compare region of one String with the specified region of another String you can use regionMatches() method. See example.
Compare Strings using equals() and equalsIgnoreCase() methods
- boolean equals(Object anObject)- Compares this string to the specified object. This method returns true if the passed argument is not null and is a String object having the same sequence of characters as this String.
- boolean equalsIgnoreCase(String anotherString)- Compares this String to another String, ignoring case considerations. Two strings are considered equal (ignoring case) if they are of the same length and corresponding characters in the two strings are equal.
public class StringComapre { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = "hello"; //returns true System.out.println("str1.equals(str2)-" + str1.equals(str2)); //returns false as case is different System.out.println("str1.equals(str3)-" + str1.equals(str3)); } }Output
str1.equals(str2)-true str1.equals(str3)-falseJava example using equalsIgnoreCase() method
public class StringComapre { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = "hello"; //returns true System.out.println("str1.equals(str2)-" + str1.equals(str2)); //returns true as case is ignored System.out.println("str1.equalsIgnoreCase(str3)-" + str1.equalsIgnoreCase(str3)); } }Output
str1.equals(str2)-true str1.equalsIgnoreCase(str3)-true
Compare Strings using compareTo() and compareToIgnoreCase() methods
- int compareTo(String anotherString)- Compares two strings lexicographically. Returns positive integer if this String is greater than the argument, returns negative integer if this String is less than the argument, returns zero if this string is equal to the argument.
- int compareToIgnoreCase(String str)- Compares two strings lexicographically, ignoring case differences. Returns positive integer if this String is greater than the argument, returns negative integer if this String is less than the argument, returns zero if this string is equal to the argument, ignoring case considerations.
public class StringComapre { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = "Halo"; // returns 0 System.out.println("str1.compareTo(str2): " + str1.compareTo(str2)); // returns positive integer System.out.println("str1.compareTo(str3): " + str1.compareTo(str3)); // returns negative integer System.out.println("str3.compareTo(str1): " + str3.compareTo(str1)); } }Output
str1.compareTo(str2): 0 str1.compareTo(str3): 4 str3.compareTo(str1): -4
Since both str1 and str2 have the same value so 0 is returned on comparing them. On comparing str1 with str3 positive integer (4) is returned as "hello" comes after "halo".
Java example using compareToIgnoreCase() methodpublic class StringComapre { public static void main(String[] args) { String str1 = "Hello"; String str2 = "hello"; String str3 = "cello"; // returns 0 System.out.println("str1.compareTo(str2): " + str1.compareToIgnoreCase(str2)); // returns positive integer System.out.println("str1.compareTo(str3): " + str1.compareTo(str3)); // returns negative integer System.out.println("str3.compareTo(str1): " + str3.compareTo(str1)); } }Output
str1.compareTo(str2): 0 str1.compareTo(str3): -27 str3.compareTo(str1): 27
Compare String portions using startsWith() and endsWith() methods
- boolean startsWith(String str)- Tests if this string starts with the passed argument. Returns true if substring matches at the start, false otherwise.
- boolean startsWith(String str, int toffset)- Tests if the substring of this string beginning at the specified index starts with the passed argument. Returns true if substring matches at the start, false otherwise.
- boolean endsWith(String str)- Tests if this string ends with the passed argument. Returns true if substring matches at the end, false otherwise.
public class StringComapre { public static void main(String[] args) { String str = "Compare this String"; // returns true System.out.println("str.startsWith(\"Compare\"): " + str.startsWith("Compare")); // returns false System.out.println("str.startsWith(\"Comparison\"): " + str.startsWith("Comparison")); // returns true- Comparison starts from index 8 System.out.println("str.startsWith(\"this\"): " + str.startsWith("this", 8)); // returns true System.out.println("str.endsWith(\"String\"): " + str.endsWith("String")); // returns false System.out.println("str.endsWith(\"Sting\"): " + str.endsWith("Sting")); } }Output
str.startsWith("Compare"): true str.startsWith("Comparison"): false str.startsWith("this"): true str.endsWith("String"): true str.endsWith("Sting"): false
Compare String portions using regionMatches method
- boolean regionMatches(int toffset, String other, int ooffset, int len)- Tests if two string regions are equal. A substring of the first string is compared to the substring of the second string. Index from which the substring of the first string starts is specified using toffset. Index from which the substring of the second string starts is specified using ooffset. Length of the substring which is to be compared is specfied using len.
- boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)- If ignoreCase is passed as true, ignore case when comparing characters.
public class StringComapre { public static void main(String[] args) { String str1 = "Compare this string"; String str2 = "Compare with this String"; // Comparing "this" portion of both Strings- true System.out.println("str1.regionMatches(8, str2, 13, 4): " + str1.regionMatches(8, str2, 13, 4)); // Comparing "String" portion of both Strings- false when case is considered System.out.println("str1.regionMatches(13, str2, 18, 6): " + str1.regionMatches(13, str2, 18, 6)); // Comparing "String" portion of both Strings- true when case is ignored System.out.println("str1.regionMatches(true, 13, str2, 18, 6): " + str1.regionMatches(true, 13, str2, 18, 6)); } }Output
str1.regionMatches(8, str2, 13, 4): true str1.regionMatches(13, str2, 18, 6): false str1.regionMatches(true, 13, str2, 18, 6): true
That's all for the topic Compare Two Strings in Java - equals, compareTo() methods . If something is missing or you have something to share about the topic please write a comment.
You may also like
- Search String in Another String in Java - indexOf, lastIndexOf, contains methods
- Java String length() Method With Examples
- Convert Char to String And String to Char in Java
- Java Finally Block - Exception Handling
- How to Iterate a Java HashMap
- Java CopyOnWriteArrayList With Examples
- What is HDFS Federation in Hadoop
- Spring Data Query Methods
No comments:
Post a Comment