How to compare a CharSequence to a String - Android
Solution 1
Use .equals
or .equalsIgnoreCase
to compare strings
if(p.getDescs().toString().equals("trash"))
Also check this
What is the difference between == vs equals() in Java?
Solution 2
Intro
A CharSequence
is a general type that includes other types like String
, StringBuilder
, SpannableString
, and others. If you are comparing two strings you can just use .equals
(see this question). But if you are comparing a String
to a CharSequence
, you have to decide what it is you want to compare, since you might be comparing a String
to a StringBuilder
or a SpannableString
or something else.
Compare content only
If you only want to compare the content (ie, text) of a String
to the content of the CharSequence
, but you don't care about the type, then you can use String.contentEquals()
.
String myString = "hello";
CharSequence myCharSequence = new StringBuilder("hello");
boolean areEqual = myString.contentEquals(myCharSequence); // true
Compare content and type
If you also want to make sure the types are the same in addition to the content, then use String.equals()
.
String myString = "hello";
CharSequence myCharSequence = new StringBuilder("hello");
boolean areEqual = myString.equals(myCharSequence); // false
Here it is again with the same types.
String myString = "hello";
CharSequence myCharSequence = "hello"; // String
boolean areEqual = myString.equals(myCharSequence); // true
See also
- CharSequence VS String in Java?
- Android Spanned, SpannedString, Spannable, SpannableString and CharSequence
- Difference between String#equals and String#contentEquals methods
Solution 3
You need to use the equals()
method for String comparison...
if(p.getDescs().toString().equals("trash")){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
Refer to How do I compare strings in Java? for more information.
Solution 4
If descs is your CharSequence variable:
descs.toString().equals("SomeString");
Solution 5
The Best method should be like thi s
if("trash".equals(p.getDescs()){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
Alternatively if comparison is not case sensitive then
if("trash".equalsIgnoreCase(p.getDescs()){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
The function
<String>.equalus(Object)
Compare any string varialbe to value of Object varialble
Note: Because the value of p.getDescs()
could be null
so using
if(p.getDescs().toString().equals("trash")){
descsView.setVisibility(View.GONE);
}
else{
descsView.setText(p.getDescs());
}
may through error on Runtime
following links may help
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 07, 2022Comments
-
Admin almost 2 years
I have a class that places a text into an arrayList. Then it makes all the variables into CharSequences, when I try to compare the variables to a String such as == "test"; it doesn't work here is my code that I use to get the variables
class Item { String descs; public Item (String descs){ this.descs = descs; } public CharSequence getDescs() { return descs; } }
This is the code that compares it to the String
if(p.getDescs().toString() == "trash"){ descsView.setVisibility(View.GONE); } else{ descsView.setText(p.getDescs()); }
I know for a fact that p.getDescs() is equal to trash because when it sets the text for descsView it is set to trash. SO why doesn't the first if statement work?
-
Ole V.V. over 4 yearsI wasn’t aware of
contentEquals()
before. It’s useful here. Plus one.