Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Collator equals(String, String) method in Java with Example
Next article icon

Java String contentEquals() Method with Examples

Last Updated : 13 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Next Article
Collator equals(String, String) method in Java with Example
author
harshsethi2000
Improve
Article Tags :
  • Java
  • Java-Strings
Practice Tags :
  • Java
  • Java-Strings

Similar Reads

  • Java String contains() Method with Example
    The String.contains() method is used to search for a sequence of characters within a string. In this article, we will learn how to effectively use the string contains functionality in Java. Example: In this example, we check if a specific substring is present in the given string. [GFGTABS] Java // J
    3 min read
  • Java String compareTo() Method with Examples
    Strings in Java are objects that are supported internally by an array only which means contiguous allocation of memory for characters. Please note that strings are immutable in Java which means once we create a String object and assign some values to it, we cannot change the content. However, we can
    7 min read
  • StringWriter equals() method in Java with Example
    The Java.io.StringWriter.equals(Object obj) method of StringWriter class in Java is used to check whether the two instances of StringWriter are equal or not. It returns a boolean stating whether they are equal or not. Signature: public boolean equals(StringWriter second_StringWriter) Syntax: first_S
    2 min read
  • Short equals() method in Java with Examples
    The equals() method of Short class is a built in method in Java which is used to compare the equality given Object with the instance of Short invoking the equals() method. Syntax ShortObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the insta
    2 min read
  • Collator equals(String, String) method in Java with Example
    The equals() method of java.text.Collator class is used to check if both strings are identical or not. Syntax: public boolean equals(String source, String target) Parameter: This method takes two strings between which comparison is going to take place. Return Value: if both strings are equal to each
    2 min read
  • Optional equals() method in Java with Examples
    The equals() method of java.util.Optional class in Java is used to check for equality of this Optional with the specified Optional. This method takes an Optional instance and compares it with this Optional and returns a boolean value representing the same. Syntax: public boolean equals(Object obj) P
    2 min read
  • Constructor equals() method in Java with Examples
    The constructor class provides information about a single constructor for a class and it also provides access to that constructor. The equals() method of java.lang.reflect.Constructor is used to compare this Constructor against the passed object. If the objects are the same then the method returns t
    2 min read
  • BitSet equals() Method in Java with Examples
    The equals() method of Java BitSet class is used to check for equality between two bitsets. It verifies whether the elements of one set passed as a parameter is equal to the elements of this set or not. The method returns true if the bitsets match else false. Syntax: Bit_Set1.equals(Bit_Set2) Parame
    2 min read
  • Period equals() method in Java with Examples
    The equals() method of Period class in Java is used to check if two given periods are equal or not. The comparison is based on the type Period and each of the three years, months and date. To be equal, all of the three year, month and date must be individually equal. Syntax: public boolean equals(Pe
    2 min read
  • Set contains() method in Java with Examples
    The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences