String methods – contentEquals

ContentEquals() is used to compare the string. It has 2 types of implementations.

  1. boolean contentEquals(CharSequence cs)
  2. 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

 

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
Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...