The method contentEquals()
compares the String
with the String Buffer
and returns a boolean value. It returns true if the String matches to the String buffer else it returns false.
boolean contentEquals(StringBuffer sb)
Example
In this example we have two Strings and two String Buffers. We are comparing the Strings with String Buffers using the contentEquals()
method. Here we are displaying the result by directly calling the method in System.out.println
statement. However you can also store the returned value in a boolean variable and use it further like this: boolean var = str1.contentEquals(sb1);
public class ContentEqualsExample { public static void main(String args[]) { String str1 = "First String"; String str2 = "Second String"; StringBuffer str3 = new StringBuffer( "Second String"); StringBuffer str4 = new StringBuffer( "First String"); System.out.println("str1 equals to str3:"+str1.contentEquals(str3)); System.out.println("str2 equals to str3:"+str2.contentEquals(str3)); System.out.println("str1 equals to str4:"+str1.contentEquals(str4)); System.out.println("str2 equals to str4:"+str2.contentEquals(str4)); } }
Output:
str1 equals to str3:false str2 equals to str3:true str1 equals to str4:true str2 equals to str4:false
Leave a Reply