Question

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?

Was it helpful?

Solution

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?

OTHER TIPS

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

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.

If descs is your CharSequence variable:

descs.toString().equals("SomeString");

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

How to compare string values of CharSequence[]?

CharSequence vs String

The currently accepted answer

if(p.getDescs().toString().equals("trash"))

can be simplified to

if("trash".contentEquals(p.getDescs()))

Suragch's "Intro" answer gives the details. This is the way to directly compare a CharSequence to a String, which is what the OP asked for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top