Compare a String with StringBuffer object

contentEquals(StringBuffer cs) method of String class can be used to compare a string with StringBuffer object.

This method returns true if the character sequence or stringbuffer value is equal to the string and false if character sequence or stringbuffer value is not equal to the String. i.e return type is boolean.


Example 1

class StringTest{

public static void main(String args[]){

String str1="ProgramZools";

StringBuffer str2=new StringBuffer("ProgramZools");

if(str1.contentEquals(str2)==true){

System.out.println("Both are equal");

}

else{

System.out.println("Both are different");

}

}

}

Output 1

Both are equal


Example 2

class StringTest{

public static void main(String args[]){

String str1="ProgramZools";

if(str1.contentEquals("ProgramZools")==true){

System.out.println("Both are equal");

}

else{

System.out.println("Both are different");

}

}

}

Output 2

Both are equal