Skip to content Skip to sidebar Skip to footer

Why Is My String To String Comparison Failing?

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 below: pri

Solution 1:

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.


Solution 2:


Solution 3:

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;
}

Post a Comment for "Why Is My String To String Comparison Failing?"