➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
StringUtils equals() Method | In this blog, we will discuss the equals() method of the StringUtils class. The StringUtils.equals() method compares the two different strings and evaluates whether they are equal or not. The StringUtils.equals() method returns true if the strings are equal or else it returns false. The return type of the equals method is boolean.
Java StringUtils equals() Method
The syntax for the equals method is as follows:-
public static boolean equals(final CharSequence cs1, final CharSequence cs2)
It compares two CharSequences, returning true if they represent equal sequences of characters. The nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case-sensitive.
Parameters:
cs1:- the CharSequence needed to be compared.
cs2:- the CharSequence which is compared.
Return type: boolean
Returns: true or false:- true if the strings are equal and false if the strings are not equal.
An example of the StringUtils equals() method is as follows:-
Example1:
string1: “Hi”
string2: “People”
Compare string1 and string2: false
Example2:
string1: “Hi”
string2: “Hi”
Compare string1 and string2: true.
There are some other similar variations of the StringUtils.equals() method:-
- public static boolean equalsIgnoreCase(final CharSequence cs1, final CharSequence cs2)
- equalsAny(CharSequence string, CharSequence… searchStrings)
- equalsAnyIgnoreCase(CharSequence string, CharSequence… searchStrings)
StringUtils equals() Java Example
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String string1 = null;
String string2 = null;
System.out.println(StringUtils.equals(string1, string2));
string1 = "Know Program";
string2 = "Know Program";
System.out.println(StringUtils.equals(string1, string2));
}
}
Output:-
true
true
Initially both string variables referenced to null therefore StringUtils.equals(string1, string2) returns true, and later both string contains same value/data therefore StringUtils.equals() method returns true.
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String string1 = null;
String string2 = "Hi";
System.out.println(StringUtils.equals(string1, string2));
string1 = "Hello";
System.out.println(StringUtils.equals(string1, string2));
}
}
Output:-
false
false
The string variable “string1” is referenced to the null but the string variable “string2” contains the string literal “Hi”. Since both are not similar and don’t contain the same characters therefore the StringUtils.equals() method returns false.
Later string variable “string1” contains the string literal “Hello” which is completely different from the string literal “Hi” therefore StringUtils.equals(string1, string2) method returns false.
StringUtils equalsIgnoreCase() Method Example
The StringUtils equals() method compare CharSequence case wise. If we want to ignore the case of string/character then we can use the StringUtils equalsIgnoreCase() Method.
The syntax of StringUtils.equalsIgnoreCase() is as follows:-
public static boolean equalsIgnoreCase(final CharSequence cs1, final CharSequence cs2)
It compares two CharSequences, returning true if they represent equal sequences of characters, ignoring the case.
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String string1 = "Hello";
String string2 = "HELLO";
System.out.println(StringUtils.equals(string1, string2));
System.out.println(StringUtils.equalsIgnoreCase(string1, string2));
}
}
Output:-
false
true
StringUtils equalsAny() Method
If we have many strings and we want to check whether in these strings there is a specific string, in that case, we can use StringUtils equalsAny() method.
The syntax for StringUtils equalsAny() method is as follows:
public static boolean equalsAny(final CharSequence string, final CharSequence… searchStrings)
It compares the given string to a CharSequences vararg of searchStrings, returning true if the string is equal to any of the searchStrings.
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String string = "Hi";
System.out.println(StringUtils.equalsAny(string, "Hello", "Hey", "Welcome"));
System.out.println(StringUtils.equalsAny(string, "Hello", "Hi", "Welcome"));
}
}
Output:-
false
true
In the first call of StringUtils.equalsAny() the string vararg doesn’t contain the “Hi” string therefore the StringUtils.equalsAny() method returns false but in the second call, the string vararg contains the “Hi” string therefore the StringUtils.equalsAny() method return true.
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String string = "xyz";
System.out.println(
StringUtils.equalsAny(string, "XYZ", "xy", "yz", "x", "y", "z"));
System.out.println(
StringUtils.equalsAny(string, "XYZ", "xyz", "yz", "x", "y", "z"));
}
}
Output:-
false
true
StringUtils equalsAnyIgnoreCase() Method
See the below program which explains the other variation of the equals method that is equalsAnyIgnoreCase. The syntax for StringUtils equalsAnyIgnoreCase() method is as follows:-
public static boolean equalsAnyIgnoreCase(CharSequence string, CharSequence… searchStrings)
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String string = "xyz";
System.out.println(
StringUtils.equalsAnyIgnoreCase(string, "wxyz", "xy", "yz", "x", "y", "z"));
System.out.println(
StringUtils.equalsAnyIgnoreCase(string, "XYZ", "xy", "yz", "x", "y", "z"));
System.out.println(
StringUtils.equalsAnyIgnoreCase(string, "XYZ", "xyz", "yz", "x", "y", "z"));
}
}
Output:-
false
true
true
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!