In Java, a sequence of characters is known as a string. It is immutable (cannot be changed once it is created) and helps in performing several operations. Also, a Comparison of String is a common programming task in Java. It can be performed using several ways, and it will be discussed in the following sections in detail.
As already discussed, a String comparison can be done using different methods. They are:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
equals() method compares two strings based on the string content. If the strings are not in the same case (i.e. upper or lower case), it will be considered as not equal. Below is an example of the equals() method.
Program
public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; //string comparison using equals method if (str1.equals(str2)) { System.out.println("Both str1 : " + str1 + " and str2 : "+ str2 +" are equal"); } else { System.out.println("Both str1 : " + str1 + " and str2 : "+ str2 +" are not equal"); } String str3 = "Happy"; String str4 = "hai"; if (str3.equals(str4)) { System.out.println("Both str3 : " + str3 + " and str4 : "+ str4 +" are equal"); } else { System.out.println("Both str3 : " + str3 + " and str4 : "+ str4 +" are not equal"); } String str5 = "Happy"; String str6 = "hard"; if (str5.equals(str6)) { System.out.println("Both str5 : " + str5 + "and str6 : "+ str6 +" are equal"); } else { System.out.println("Both str5 : " + str5 + " and str6 : "+ str6 +" are not equal"); } } }
Output:
Explanation of the above code
In this method, values are compared lexicographically and return a value of integer type. The value is based on whether the first string is equal to, less than or greater than the 2ndstring.
Two strings str1 and str2 are present and
Program
//Java program to demonstrate compareTo method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; String str3 = "Happy"; String str4 = "hai"; //string comparison using compareTo method System.out.println(str1.compareTo(str2)); System.out.println(str3.compareTo(str4)); String str5 = "Happy"; String str6 = "Hardest one"; System.out.println(str5.compareTo(str6)); } }
Output:
Explanation of the above code
Using this method, two strings will be compared without considering whether the string is upper case or lower case.
Program
//Java program to demonstrate equalsIgnoreCase method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "balloon"; //string comparison using equalsIgnoreCase method System.out.println(str1.equalsIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println(str3.equalsIgnoreCase(str4)); String str5 = "Happy"; String str6 = "hard"; System.out.println(str5.equalsIgnoreCase(str6)); } }
Output:
Explanation of the above code
This method is similar to the compareTo method, where strings are compared lexicographically. The difference is that comparison won’t be affected whether the strings are in upper case or lower case.
Program
//Java program to demonstrate compareToIgnoreCase method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "balloon"; //string comparison using compareToIgnoreCase method System.out.println(str1.compareToIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println(str3.compareToIgnoreCase(str4)); String str5 = "Happy"; String str6 = "Hard"; System.out.println(str5.compareToIgnoreCase(str6)); } }
Output:
Explanation of the above code
Now, let us see all the above-mentioned methods in a single program to better understand string comparison.
Program
//Java program to demonstrate different methods for string comparison public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; System.out.println("Comparison of str1 : " + str1 + " and str2 : " + str2); System.out.println("Using equals method "); //string comparison using equals method if (str1.equals(str2)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); //string comparison using compareTo method System.out.println(str1.compareTo(str2)); System.out.println("Using equalsIgnoreCase method "); //string comparison using equalsIgnoreCase method System.out.println(str1.equalsIgnoreCase(str2)); System.out.println("Using compareToIgnoreCase method "); //string comparison using compareToIgnoreCase method System.out.println(str1.compareToIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println("\nComparison of str3 : " + str3 + " and str4 : " + str4); System.out.println("Using equals method "); if (str3.equals(str4)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); System.out.println(str3.compareTo(str4)); System.out.println("Using equalsIgnoreCase method "); System.out.println(str3.equalsIgnoreCase(str4)); System.out.println("Using compareToIgnoreCase method "); System.out.println(str3.compareToIgnoreCase(str4)); String str5 = "Happy"; String str6 = "hard"; System.out.println("\nComparison of str5 : " + str5 + " and str6 : " + str6); System.out.println("Using equals method "); if (str5.equals(str6)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); System.out.println(str5.compareTo(str6)); System.out.println("Using equalsIgnoreCase method "); System.out.println(str5.equalsIgnoreCase(str6)); System.out.println("Using compareToIgnoreCase method "); System.out.println(str5.compareToIgnoreCase(str6)); } }
Output:
A string is a sequence of characters, and its objects are immutable. There are different methods such as equals, compareTo, etc., available in order to compare the strings. All these methods are used based on the requirements. They are explained in the above section in detail.
The above is the detailed content of String Comparison in Java. For more information, please follow other related articles on the PHP Chinese website!