Java String contentEquals() Method Example

The String.contentEquals() method in Java is used to compare a String with a CharSequence or a StringBuffer to check if they have the same content. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. contentEquals Method Syntax
  3. Examples
    • Comparing String with CharSequence
    • Comparing String with StringBuffer
    • Handling Different Content
  4. Real-World Use Case
  5. Conclusion

Introduction

The String.contentEquals() method is part of the String class in Java. It is used to compare the content of a String with a CharSequence or StringBuffer. This method is useful when you need to check if two character sequences have identical content.

contentEquals Method Syntax

There are two overloaded versions of the contentEquals method:

  1. To compare a String with a CharSequence:

    public boolean contentEquals(CharSequence cs)
    
  2. To compare a String with a StringBuffer:

    public boolean contentEquals(StringBuffer sb)
    

Examples

Comparing String with CharSequence

The contentEquals(CharSequence cs) method can be used to compare a String with a CharSequence.

Example

public class ContentEqualsExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        CharSequence cs = "Hello, World!";

        boolean isEqual = str.contentEquals(cs);

        System.out.println("String and CharSequence are equal: " + isEqual);
    }
}

Output:

String and CharSequence are equal: true

Comparing String with StringBuffer

The contentEquals(StringBuffer sb) method can be used to compare a String with a StringBuffer.

Example

public class ContentEqualsStringBufferExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        StringBuffer sb = new StringBuffer("Hello, World!");

        boolean isEqual = str.contentEquals(sb);

        System.out.println("String and StringBuffer are equal: " + isEqual);
    }
}

Output:

String and StringBuffer are equal: true

Handling Different Content

If the contents of the String and CharSequence or StringBuffer are different, the method will return false.

Example

public class ContentEqualsDifferentContentExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        CharSequence cs = "Hello, Java!";
        StringBuffer sb = new StringBuffer("Hello, Java!");

        boolean isEqualCs = str.contentEquals(cs);
        boolean isEqualSb = str.contentEquals(sb);

        System.out.println("String and CharSequence are equal: " + isEqualCs);
        System.out.println("String and StringBuffer are equal: " + isEqualSb);
    }
}

Output:

String and CharSequence are equal: false
String and StringBuffer are equal: false

Real-World Use Case

Verifying User Input

In a web application, you might need to verify if user input matches a predefined string, such as a verification code or password. The contentEquals() method can be used to compare the user input with the stored value.

Example

public class UserInputVerification {
    public static void main(String[] args) {
        String storedCode = "12345";
        CharSequence userInput = "12345";

        boolean isVerified = storedCode.contentEquals(userInput);

        if (isVerified) {
            System.out.println("User input is verified.");
        } else {
            System.out.println("User input is incorrect.");
        }
    }
}

Output:

User input is verified.

Conclusion

The String.contentEquals() method in Java provides a convenient way to compare the content of a String with a CharSequence or StringBuffer. By understanding how to use this method, you can efficiently verify if two character sequences have identical content in your Java applications. This method is particularly useful in scenarios such as verifying user input against stored values.


Comments