Java String contentEquals() Method Tutorial

In this section, we will learn what the String contentEquals() method is and how to use it in Java.

What is Java String contentEquals() Method?

The Java String contentEquals() method is used to check two string values and see if they are equally the same in content or not.

Basically, the `contentEquals()` method takes one argument that is of type `CharSequence` interface or `StringBuffer` class and compares the value of the argument with the target String object to see if they match or not.

Java contentEquals() Method Syntax:

public boolean contentEquals(StringBuffer chars)

public boolean contentEquals(CharSequence chars)

contentEquals() Method Parameters:

This method takes one argument, and that is the value we want to compare with the target string value to see if they are equal in content or not.

Notes:

  • The `CharSequence` is an interface which the `String` class implement it. So we can use String objects as the argument to this method.
  • The `StringBuffer` class is like the `String` class and is used to store character string. The difference, however, is that the content of objects of this type are editable. Also, this class implemented the `CharSequence` interface.

contentEquals() Method Return Value:

The return value of this method is a boolean value:

If both the argument and the target string object were the same, the result will be `true` otherwise the result is `false`.

Example: using String contentEquals() method

public class Simple {

    public static void main(String[] args) {
        String s = "Life";
        System.out.println(s.contentEquals("Life"));
        System.out.println(s.contentEquals("Li"));
        System.out.println(s.contentEquals(new StringBuffer("Lif")));
    }
}

Output:

true

false

false

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies