In Java, contentEquals() checks whether this string has the same characters as a StringBuilder, StringBuffer, or other CharSequence.
You pass the other sequence. It is required. The compare is case-sensitive. Use this when equals() would fail because the other object is not a String.
Syntax
Call contentEquals on the string. Start with this line.
java
boolean ok = text.contentEquals(sb);
Call
What it does
text.contentEquals(sb)
Returns true when text and sb have the same characters.
equals(sb) is false when sb is a StringBuilder, even if the characters match.
Library declarations
java
public boolean contentEquals(StringBuffer sb);
public boolean contentEquals(CharSequence cs);
Parameters
Parameter
Description
cs
Required. The StringBuilder, StringBuffer, or CharSequence to compare.
Return Value
Return Value
Description
true
The characters match.
false
The characters do not match.
Java Examples
Example 1 — Match a StringBuilder
Compare a fixed string with text built in a buffer.
java
public class ContentEqualsBasic {
public static void main(String[] args) {
String expected = "Code";
StringBuilder built = new StringBuilder();
built.append("Co").append("de");
boolean ok = expected.contentEquals(built);
System.out.println("contentEquals = " + ok);
}
}
Output
contentEquals = true
How It Works
Start with a target.expected holds "Code".
Build the same text. Append "Co" then "de" into a StringBuilder.
Call contentEquals. Characters are compared in order.
Get true. No toString() was required.
Example 2 — equals vs contentEquals
equals() rejects a builder even when the text matches.
java
public class ContentEqualsVsEquals {
public static void main(String[] args) {
String s = "Hi";
StringBuilder sb = new StringBuilder("Hi");
System.out.println("equals = " + s.equals(sb));
System.out.println("contentEquals = " + s.contentEquals(sb));
}
}
Output
equals = false
contentEquals = true
How It Works
Same characters. Both hold H then i.
equals fails. The argument is not a String, so content is never compared.
contentEquals succeeds. It accepts any CharSequence.
Choose by type. Use equals for two strings; use contentEquals for builders.
Example 3 — Case Still Matters
Content match is exact. For ignore-case on two strings, use equalsIgnoreCase().
java
public class ContentEqualsCase {
public static void main(String[] args) {
String s = "Java";
StringBuilder sb = new StringBuilder("java");
System.out.println(s.contentEquals(sb));
System.out.println(s.equalsIgnoreCase(sb.toString()));
}
}
Output
false
true
How It Works
Different case."Java" vs "java".
contentEquals is exact.'J' ≠ 'j' → false.
Need ignore-case? Convert the builder (or use two strings) and call equalsIgnoreCase.
Pick the right tool. Content match ≠ case-insensitive match.
Frequently Asked Questions
contentEquals() compares this string's characters with a StringBuilder, StringBuffer, or CharSequence.
cs is required. It is the other character sequence.
equals() expects another String. contentEquals() can compare with builders and other CharSequence values.