Java String contentEquals() Method

The String.contentEquals() method in Java is used to compare a String to a StringBuffer or CharSequence to determine if they have the same sequence of characters. 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 with StringBuffer
    • Comparing with StringBuilder
    • Comparing with CharSequence
    • Handling Edge Cases
  4. Conclusion

Introduction

The String.contentEquals() method is a member of the String class in Java. It allows you to compare a string with a StringBuffer or any CharSequence to check if they contain the same sequence of characters. This method is particularly useful for comparing different types of character sequences for equality.

contentEquals Method Syntax

The contentEquals method has two common variations:

  1. Comparing with StringBuffer:
public boolean contentEquals(StringBuffer sb)
  1. Comparing with CharSequence:
public boolean contentEquals(CharSequence cs)
  • sb: The StringBuffer to be compared.
  • cs: The CharSequence to be compared.

Examples

Comparing with StringBuffer

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

Example

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

        boolean isEqual = str.contentEquals(stringBuffer);

        System.out.println("String equals StringBuffer: " + isEqual);
    }
}

Output:

String equals StringBuffer: true

Comparing with StringBuilder

The contentEquals method can also be used to compare a String with a StringBuilder since StringBuilder implements CharSequence.

Example

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

        boolean isEqual = str.contentEquals(stringBuilder);

        System.out.println("String equals StringBuilder: " + isEqual);
    }
}

Output:

String equals StringBuilder: true

Comparing with CharSequence

The contentEquals method can be used to compare a String with any CharSequence.

Example

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

        boolean isEqual = str.contentEquals(charSequence);

        System.out.println("String equals CharSequence: " + isEqual);
    }
}

Output:

String equals CharSequence: true

Handling Edge Cases

The contentEquals method returns false if the compared StringBuffer or CharSequence is not equal to the string.

Example

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

        boolean isEqual = str.contentEquals(stringBuffer);

        System.out.println("String equals StringBuffer: " + isEqual);
    }
}

Output:

String equals StringBuffer: false

Conclusion

The String.contentEquals() method in Java is used for comparing a string with a StringBuffer or any CharSequence. By understanding how to use this method, you can efficiently compare different types of character sequences for equality in your Java applications. Whether you are comparing with StringBuffer, StringBuilder, or other CharSequence implementations, the contentEquals method provides a reliable solution for these tasks.

Comments