Introduction
The contentEquals()
method in Java provides a way to compare the content of two strings or character sequences. Unlike the standard equals()
method, which considers Unicode values and normalization forms, contentEquals()
performs a simpler comparison based on characters.
Syntax
public boolean contentEquals(CharSequence cs)
public boolean contentEquals(StringBuffer sb)
Parameters
Parameter | Description |
---|---|
cs | The character sequence to compare with this string. Can be a String, StringBuffer, CharArray, or any other object implementing the CharSequence interface. |
sb | The StringBuffer object to compare with this string. |
Return Value
This method returns true
if the content of the two sequences are equal; otherwise, it returns false
.
Examples
Comparing Strings with contentEquals()
Let's start by looking at a straightforward example where we compare two strings using contentEquals()
. We'll also compare it to the standard equals()
method to highlight the differences.
import java.util.*;\n
public class ContentEqualsExample {\n public static void main(String[] args) {\n String str1 = "Hello";\n String str2 = "Hello";\n String str3 = "hello";\n
System.out.println("str1.contentEquals(str2): " + str1.contentEquals(str2));\n System.out.println("str1.contentEquals(str3): " + str1.contentEquals(str3));\n System.out.println("str1.equals(str2): " + str1.equals(str2));\n System.out.println("str1.equals(str3): " + str1.equals(str3));\n }\n}
str1.contentEquals(str2): true
str1.contentEquals(str3): false
str1.equals(str2): true
str1.equals(str3): false
In this example, both contentEquals()
and equals()
return true
when comparing str1
and str2
because they contain the same characters in the same order. However, when comparing str1
(which is "Hello") with str3
(which is "hello"), both methods correctly identify them as different due to case sensitivity.
Comparing a String with a StringBuffer
Now, let's see how contentEquals()
behaves when comparing a String
with a StringBuffer
. They are distinct classes, but their content can still be compared.
import java.util.*;\n
public class StringBufferContentEqualsExample {\n public static void main(String[] args) {\n String str = "Java";\n StringBuffer sb = new StringBuffer("Java");\n
System.out.println("str.contentEquals(sb): " + str.contentEquals(sb));\n }\n}
str.contentEquals(sb): true
Here, contentEquals()
returns true
because the string "Java" and the StringBuffer
containing "Java" have identical content.
Comparing with a CharSequence
You can also compare using any object that implements CharSequence
. This provides flexibility when dealing with different character sequence representations.
import java.util.*;
public class CharSequenceContentEqualsExample {
public static void main(String[] args) {\n String str = "Test";\n CharSequence cs = "Test"; // String implements CharSequence
System.out.println("str.contentEquals(cs): " + str.contentEquals(cs));
}
}
str.contentEquals(cs): true
Key Differences and When to Use contentEquals()
The main difference between contentEquals()
and equals()
lies in their handling of Unicode normalization forms. If you need a simple, character-by-character comparison without considering normalization issues, contentEquals()
is generally faster and more appropriate.
Use contentEquals()
when:
- You want a quick and straightforward content comparison.
- Unicode normalization forms are not relevant to your comparison logic.