Why is my String to String comparison failing?

后端 未结 3 349
醉酒成梦
醉酒成梦 2020-12-11 16:09

I have an Android app where I want to check to see if an app name that is installed matches a string passed to the function containing this code. The code and example is bel

相关标签:
3条回答
  • 2020-12-11 16:42
    public static boolean compaireString (String string, String string2) 
    {
        // string == null && String2 == null or they reference the same object
        if (string == string2) return true;
        //we have to be sure that string is not null before calling a methode on it
        if (string != null && string.equals(string2)) return true;
    
       return false;
    }
    
    0 讨论(0)
  • 2020-12-11 16:58

    Check out number 7: Top 10 - New Java Developer Errors

    0 讨论(0)
  • 2020-12-11 16:59

    Use the String's equals() method instead of the == operator for comparing strings:

    info.activityInfo.applicationInfo.loadLabel( pm ).toString().equals(appName)
    

    In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object references, not the content.

    0 讨论(0)
提交回复
热议问题