Skip to content
Related Articles

Related Articles

Java String contentEquals() Method with Examples

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 13 May, 2022
Improve Article
Save Article

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:

  1. contentEquals(StringBuffer sb)
  2. contentEquals(CharSequence cs)

1. contentEquals(StringBuffer sb):

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

How to invoke contentEquals(StringBuffer sb) method?

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));
    }
}


Output

String one equals to specified StringBuffer : true
String two equals to specified StringBuffer : false

2. contentEquals(CharSequence cs)

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

How to invoke contentEquals(CharSequence cs) method?

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));
    }
}


Output

String one equals to specified sequence : true
String two equals to specified sequence : false

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!