String methods – contentEquals
ContentEquals() is used to compare the string. It has 2 types of implementations.
- boolean contentEquals(CharSequence cs)
- boolean contentEquals(StringBuffer sb)
Let’s understand them with the help of examples.
boolean contentEquals(CharSequence cs):
Returns true if string matches with the specified sequence of characters, else returns false.
public class StringMethodsEx { public static void main(String[] args) { String val1 = "Hello world!!"; System.out.println(val1.contentEquals("Hello world!!")); //returns true System.out.println(val1.contentEquals("universe")); //returns false } }
Output:
true
false
true
false
boolean contentEquals(StringBuffer sb):
This method compares the String with String buffer. If both matches returns true, else return false.
public class StringMethodsEx { public static void main(String[] args) { String val1 = "Hello world!!"; //String buffer StringBuffer sf1 = new StringBuffer("Hello world!!"); StringBuffer sf2 = new StringBuffer("Hello universe!!"); System.out.println(val1.contentEquals(sf1)); //returns true System.out.println(val1.contentEquals(sf2)); //returns false } }
Output:
true
false
true
false
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum