⬅ Previous Topic
Java Stringcontains()
methodNext Topic ⮕
Java StringcopyValueOf()
methodcontentEquals()
method⬅ Previous Topic
Java Stringcontains()
methodNext Topic ⮕
Java StringcopyValueOf()
methodThe 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.
public boolean contentEquals(CharSequence cs)
public boolean contentEquals(StringBuffer sb)
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. |
This method returns true
if the content of the two sequences are equal; otherwise, it returns false
.
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.
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.
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
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:
⬅ Previous Topic
Java Stringcontains()
methodNext Topic ⮕
Java StringcopyValueOf()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.