Java - String contentEquals() Method



The Java String contentEquals() method is, used to compare the current string to the given character sequence. This method returns a Boolean value which is true if and only if this String represents the same sequence of char values as the specified sequence; false otherwise.

The contentEquals() method accepts two parameters, which are char Sequence and StringBuffer, that hold the cs and sb values. It does not throw any exception while comparing the current string to the specified Char-Sequence and String buffer.

The CharSequance is a readable sequence of the character values. A string is an immutable sequence of characters and implements the CharSequance interface. The StringBuffer is similar to a string. But, it is modifiable and synchronized.

The contentEquals() method has two polymorphic variants with different parameters: charSequence and StringBuffer (Below are the syntaxes of all the polymorphic variants).

Syntax

Following is the syntax of the Java String contentEquals() method −

public boolean contentEquals(CharSequence cs)  
public Boolean contentEquals(StringBuffer sb) 

Parameters

  • cs − This is the char-sequence to compare with this String.

  • sb − This is the StringBuffer sequence to compare with this Sting.

Return Value

This method returns a Boolean value which is True current string and the given object are equal and, false if not.

Example

If we pass the CharSequence as an argument to this method and it has the same value as the current string, the contentEquals() method returns true.

In the following program, we are creating a string literal with the value “Hello”. Then, we are creating a charSequence with the same value “Hello”. Since both have the same value, when we compare them using the contentEquals() method, this method returns true.

import java.lang.*;
public class TP {
   public static void main(String[] args) {
      
      // create a string literal
      String str1 = "Hello";
      System.out.println("The String is: " + str1);
      
      //create a charSequence
      CharSequence ch = "Hello";
      System.out.println("The given char sequence is: " + ch);
      System.out.println("The given string is equal to the specified sequence or not? " + str1.contentEquals(ch));
   }
}

Output

On executing the above program, it will produce the following result −

The String is: Hello
The given char sequence is: Hello
The given string is equal to the specified sequence or not? true

Example

If we pass the CharSequence as an argument to the method and it has the different values to the current string, this method returns false.

In the following program, we are creating an object of the string class with the value “TutorialsPoint”. Then, using the contentEquals() method, we are, trying to compare the string at the specified char sequence. Since both have different values, the method returns false.

import java.lang.*;
public class TP {
   public static void main(String[] args) {
      
      // create an object of the String  class
      String str = new String("TutorialsPoint");
      
      // CharSequance declaration
      CharSequence cs = "Point";
      System.out.println("The string is: " + str);
      System.out.println("The CharSequence is: " + cs);
      
      // declare boolean variable to hold the method value
      boolean bool = str.contentEquals(cs);
      System.out.println("The given string is equal to the specified sequence or not? " + bool);
   }
}

Output

Following is the output of the above program −

The string is: TutorialsPoint
The CharSequence is: Point
The given string is equal to the specified sequence or not? false

Example

If we pass the StringBuffer as an argument to the method, and it has the same value as the string, the contentEquals() method returns true.

In the following program, we are instantiating the string class with the value “Hello World”. Then, we are, creating an object of the StringBuffer class with the value “Hello World”. Using the contentEquals() method, we are trying to compare the string at the specified StringBuffer. Since both have same values, the contentEquals() method returns true.

import java.lang.*;
public class TP {
   public static void main(String[] args) {
      
      // Instantiate the String class
      String str = new String("Hello World");
      
      // create an object of the StringBuffer class
      StringBuffer sb = new StringBuffer("Hello World");
      System.out.println("The string is: " + str);
      System.out.println("The StringBuffer is: " + sb);
      
      // using contentEquals() method
      System.out.println("The given string is equal to the specified StringBuffer sequence or not? " + str.contentEquals(sb));
   }
}

Output

The above program, produces the following output −

The string is: Hello World
The StringBuffer is: Hello World
The given string is equal to the specified StringBuffer sequence or not? true

Example

In this program, we create a string literal with the value of "JavaScript". Then, we are creating a StringBuffer with the value “Java”. Using the contentEquals() method, we are trying to compare the string to the specified StringBuffer.

import java.lang.*;
public class TP {
   public static void main(String[] args) {
      
      // create a string literal
      String str ="JavaScript";
      
      // Instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("Java");
      System.out.println("The string is: " + str);
      System.out.println("The StringBuffer is: " + sb);
      
      // using contentEquals() method
      boolean bool = str.contentEquals(sb);
      if(bool) {
         System.out.println("The given string is equal to the specified StringBuffer sequence");
      } else {
         System.out.println("The given string is not equal to the specified StringBuffer sequence");
      }
   }
}

Output

After executing the above program, it generates the following output −

The string is: JavaScript
The StringBuffer is: Java
The given string is not equal to the specified StringBuffer sequence

Example

If the given string value is null, this method throws the NullPointerException.

In the following example, we are creating a string with a null value. Then, we are instantiating the StringBuffer class with the value “Java”. Using the contentEquals() method, we are trying to compare the given string to the specified StringBuffer.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      try {
         
         // create a string literal
         String str = null;
         
         // Instantiate the StringBuffer class
         StringBuffer sb = new StringBuffer("Java");
         System.out.println("The string is: " + str);
         System.out.println("The StringBuffer is: " + sb);
         
         // using contentEquals() method
         boolean bool = str.contentEquals(sb);
         if(bool) {
            System.out.println("The given string is equal to the specified StringBuffer sequence");
         } else {
            System.out.println("The given string is not equal to the specified StringBuffer sequence");
         }
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exceptipn: " + e);
      }
   }
}

Output

Following is the output of the above program −

The string is: null
java.lang.NullPointerException: Cannot invoke "String.contentEquals(StringBuffer)" because "str" is null
The StringBuffer is: Java
	at com.tutorialspoint.String.Demo.main(Demo.java:13)
Exceptipn: java.lang.NullPointerException: Cannot invoke "String.contentEquals(StringBuffer)" because "str" is null
java_lang_string.htm
Advertisements