Collator equals(String, String) method in Java with Example
Last Updated :
30 Jan, 2023
The equals() method of java.text.Collator class is used to check if both strings are identical or not.
Syntax:
public boolean equals(String source,
String target)
Parameter: This method takes two strings between which comparison is going to take place.
Return Value: if both strings are equal to each other then it will return true otherwise false.
Below are the examples to illustrate the equals() method:
Example 1:
Java
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
Collator col = Collator.getInstance();
String obj1 = "a" ;
String obj2 = "A" ;
boolean i = col.equals(obj1, obj2);
if (i)
System.out.println(obj1
+ " is equal to "
+ obj2);
else
System.out.println(obj1
+ " is not equal to "
+ obj2);
}
catch (ClassCastException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
a is not equal to A
Example 2:
Java
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
Collator col = Collator.getInstance();
String obj1 = "a" ;
String obj2 = "a" ;
boolean i = col.equals(obj1, obj2);
if (i)
System.out.println(obj1
+ " is equal to "
+ obj2);
else
System.out.println(obj1
+ " is not equal to "
+ obj2);
}
catch (ClassCastException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/Collator.html#equals-java.lang.String-java.lang.String-
Share your thoughts in the comments
Please Login to comment...