Java String contentEquals() Method with Examples

13 May, 2022
contentEquals() method of String class is used to compare the strings. There are two types of contentEquals method available in java.lang.String with different parameters:
contentEquals(StringBuffer sb)contentEquals(CharSequence cs)
contentEquals(StringBuffer sb)
contentEquals(CharSequence cs)
contentEquals(StringBuffer sb) method compares the string to the specified StringBuffer. It will return true if the String represents the same sequence of characters as the specified StringBuffer; otherwise, it will return false.
Syntax:
public boolean contentEquals(StringBuffer sb)
Return Type: It has a boolean return type that will return true if this String represents the same sequence of characters as the specified StringBuffer, otherwise will return false.
Method Parameter: It has one parameter of type StringBuffer
Step 1: First, create an instance of the StringBuffer class to compare its sequence of characters 
StringBuffer stringBuffer = new StringBuffer( "GFG is the best");
Step 2: create an instance of String, then invoke its contentEquals method
String str= "GFG is the best";
str.contentEquals(stringBuffer)
The below Java program will illustrate the use of the contentEquals(StringBuffer sb) method:
Java
// Java program to demonstrate the working// of the contentEquals(StringBuffer sb) method import java.io.*;import java.lang.*; class GFG {    public static void main(String[] args)    {        // creating instance of StringBuffer class        StringBuffer stringBuffer            = new StringBuffer("GFG is a portal for geeks");         String one = "GFG is a portal for geeks";        String two = "GFG is a portal for gamers";         // invoking contentEquals method        // for String one and two        System.out.println(            "String one equals to specified StringBuffer : "            + one.contentEquals(stringBuffer));         System.out.println(            "String two equals to specified StringBuffer : "            + two.contentEquals(stringBuffer));    }}
String one equals to specified StringBuffer : true
String two equals to specified StringBuffer : false
contentEquals(CharSequence cs) method compares the string to the specified CharSequence. It will return true if the String represents the same sequence of char value as the specified CharSequence otherwise, it will return false.
Syntax:
public boolean contentEquals(CharSequence cs)
Method Return Type: It has a boolean return type that will return true if this String represents the same sequence of char values as the specified sequence, otherwise will return false.
Parameter: It has one parameter of type CharSequence
Step 1: First, create a sequence to compare the sequence of char values
CharSequence cs = "portal for geeks"
Step 2: Create an instance of String, then invoke its contentEquals method
String str= "portal for geeks";
str.contentEquals(cs);
The below java program will illustrate the use of the contentEquals(CharSequence cs) method –
Example:
Java
// Java program to demonstrate the working// of contentEquals(CharSequence cs) method import java.io.*;import java.lang.*; class GFG {    public static void main(String[] args)    {        // creating instance of StringBuffer class        CharSequence cs            = "GFG is best website for programmer";               String one = "GFG is best website for programmer";        String two = "GFG is a portal for geeks";               // invoking contentEquals method        // for String one and two        System.out.println(            "String one equals to specified sequence : "            + one.contentEquals(cs));               System.out.println(            "String two equals to specified sequence : "            + two.contentEquals(cs));    }}
String one equals to specified sequence : true
String two equals to specified sequence : false
blalverma92
Java-Strings
Picked
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java