beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – String contentEquals() Method example

By Chaitanya Singh | Filed Under: String handling

The method contentEquals() compares the String with the String Buffer and returns a boolean value. It returns true if the String matches to the String buffer else it returns false.

boolean contentEquals(StringBuffer sb)

Example

In this example we have two Strings and two String Buffers. We are comparing the Strings with String Buffers using the contentEquals() method. Here we are displaying the result by directly calling the method in System.out.println statement. However you can also store the returned value in a boolean variable and use it further like this: boolean var = str1.contentEquals(sb1);

public class ContentEqualsExample {
   public static void main(String args[]) {
       String str1 = "First String";
       String str2 = "Second String";
       StringBuffer str3 = new StringBuffer( "Second String");
       StringBuffer str4 = new StringBuffer( "First String");
       System.out.println("str1 equals to str3:"+str1.contentEquals(str3));
       System.out.println("str2 equals to str3:"+str2.contentEquals(str3));
       System.out.println("str1 equals to str4:"+str1.contentEquals(str4));
       System.out.println("str2 equals to str4:"+str2.contentEquals(str4));
   }
}

Output:

str1 equals to str3:false
str2 equals to str3:true
str1 equals to str4:true
str2 equals to str4:false

Enjoyed this post? Try these related posts

  1. Java – String toLowerCase() and toUpperCase() Methods
  2. Java String isEmpty() method with example
  3. Java String replace(), replaceFirst() and replaceAll() method
  4. Java – String toCharArray() Method example
  5. Java String endsWith() Method with example
  6. Java String split() Method with examples

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap