The contentEquals
method is used to check if the content of the string is equal to the passed CharSequence
StringBuffer
string.contentEquals(StringBuffer sb)
string.contentEquals(CharSequence cs)
The contentEquals
method expects a StringBuffer
or CharSequence
as an arguemnt.
Any class that implements the CharSequence
class can also be passed as an argument to the contentEquals
method. Examples include CharBuffer
, Segment
, String
, StringBuffer
, and StringBuilder
.
The contentEquals
method returns true
if the content of the string and the passed StringBuffer
/CharSequence
are equal. Otherwise, it returns false
.
class ContentEquals {public static void main( String args[] ) {String str = "Java";StringBuffer sb= new StringBuffer("Java");StringBuffer sb2= new StringBuffer("JavaScript");System.out.println( "str = " + str);System.out.println( "sb = " + sb);System.out.println( "sb = " + sb2);System.out.print( "\nCallingn str.contentEquals(sb) ");System.out.println(str.contentEquals(sb));System.out.print( "\nCallingn str.contentEquals(sb2) ");System.out.println(str.contentEquals(sb2));}}
In the code above, we have:
Created a:
str
string with the value Java
.sb*
StringBuffer with the value Java
.sb2
StringBuffer with the value JavaScript
.Called the contentEquals
method on the str
object by passing sb
as an argument.
The contentEquals
method will compare the string str
to the specified StringBuffer sb
. The return value is true
if and only if str
represents the same sequence of characters as the specified StringBuffer sb
.
In our case, both the string and StringBuffer have the same sequence of characters, so we get true
as a return value.
Called the contentEquals
method on the str
object by passing the sb2
as an argument. In this case, str
's value Java
is not equal to sb2
's value JavaScript
, so false
is returned.