Java.lang Package Classes

Java String - contentEquals() Method



The java.lang.String.contentEquals() method is used to compare the given string to the specified StringBuffer. It returns true if the string represents the same sequence of characters as the specified StringBuffer, else returns false.

Syntax

public boolean contentEquals(StringBuffer sb)

Parameters

sb specify the StringBuffer to compare against the given string.

Return Value

Returns true if this String represents the same sequence of characters as the specified StringBuffer, false otherwise.

Exception

NA.

Example:

In the example below, contentEquals() method is used to check whether the given string str1 represents the same sequence of characters as StringBuffers str2 and str3.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    String str1 = "Hello World";
    StringBuffer str2 = new StringBuffer("Hello World");
    StringBuffer str3 = new StringBuffer("World");

    System.out.println(str1.contentEquals(str2)); 
    System.out.println(str1.contentEquals(str3)); 
  }
}

The output of the above code will be:

true
false

❮ Java.lang - String