Let’s learn String contentEquals(CharSequence cs) method java.
String contentEquals(CharSequence cs) method java
String contentEquals(CharSequence cs) method compares given string to the specified CharSequence.
This method returns boolean output. The output is true if and only if given String represents the same sequence of char values as the specified sequence.
Syntax
public boolean contentEquals(CharSequence cs)
Parameters
cs – The sequence to compare this String against.
Returns
true if this String represents the same sequence of char values as the specified sequence, false otherwise.
Example
In String contentEquals(CharSequence cs) method if we pass CharSequence as an argument and if that has the same value as given string then String contentEquals(CharSequence cs) method returns true.
class StringContentEqualsMethodDemo { public static void main(String[] args) { String s = "HelloWorld"; System.out.println("String is : " + s); //create a charSequence CharSequence ch = "HelloWorld"; System.out.println("Given char sequence : " + ch); System.out.println("Is Given string is equal to the specified sequence or not? " + s.contentEquals(ch)); } }
Example:
String is : HelloWorld
Given char sequence : HelloWorld
Is Given string is equal to the specified sequence or not? true
In String contentEquals(CharSequence cs) method if we pass CharSequence as an argument and if that has different value as given string then String contentEquals(CharSequence cs) method returns false.
class StringContentEqualsMethodDemo { public static void main(String[] args) { String s = new String("HelloWorld"); CharSequence ch = "Java"; System.out.println("String is : " + s); System.out.println("CharSequence is : " + ch); boolean b = s.contentEquals(ch); System.out.println("Is given string is equal to the specified sequence or not? " + b); } }
Output:
String is : HelloWorld
CharSequence is : Java
Is given string is equal to the specified sequence or not? false
Also read – do while loop in java