2015-10-22 57 views
0

我有我讓他們與此代碼如何在我的情況比較字符串(機器人)

final String encrypted_id = encryption.encryptOrNull(android_id); 
final String preLoad = preferences.getString("pur", "no"); 

爲確保他們是平等的我登錄他們這樣

Log.d("encrypted_id",encrypted_id); 
Log.d("preferences.getString",preferences.getString("pur", "no")); 

兩個String和logcat的輸出是這樣

  D/encrypted_id﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0= 
D/preferences.getString﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0= 

所以我要確保他們是平等的

NOW我想他們這樣

if(preLoad.equals(encrypted_id)) 
    { 
     Log.d("app","is premium"); 

    } 
    else { 
     Log.d("app","is not premium"); 
    } 

logcat的節目比較這對我

D/app﹕ is not premium 

什麼問題?

PS:我試過

1 . preLoad.equalsIgnoreCase(encrypted_id)

2 . preLoad.compareTo(encrypted_id)==0

+6

你試過preLoad.trim()。equals(encrypted_id.trim())??? –

+0

你可以在else中打印'encrypted_id'和'preLoad'嗎? – ThomasThiebaud

+2

使用equalignore情況下,也修剪(),然後檢查 – Pavya

回答

0

變化:

if(preLoad.equals(encrypted_id)) 
    { 
     Log.d("app","is premium"); 

    } 
    else { 
     Log.d("app","is not premium"); 
    } 

if(preLoad.trim().equals(encrypted_id.trim())) 
    { 
     Log.d("app","is premium"); 

    } 
    else { 
     Log.d("app","is not premium"); 
    } 
+0

謝謝你斯蒂芬。你救我:) – behrooz

-1

試試這個,

if(!preLoad.trim().equals(encrypted_id.trim())) 
{ 
    Log.d("app","is not premium"); 

} 
else { 
    Log.d("app","is premium"); 
} 
+0

多數民衆贊成在同樣的答案,我只是與否定,如果條件... –

+0

斯特凡是正確的,爲什麼要使用它? – behrooz

0

你可以試試這個:

if(stringsCompare(preLoad.trim(),encrypted_id.trim())) 
    { 
     Log.d("app","is premium"); 

    } 
    else { 
     Log.d("app","is not premium"); 
    } 

stringsCompare是我寫的字符串比較的過程:

public boolean stringsCompare(String firstString, String secondString){ 
      if(firstString.length() != secondString.length()){ 
       //The length of the two strings are not equal 
       return false; 
      }else{ 
       for(int i = 0; i < firstString.length(); i++) 
       { 
        if (firstString.charAt(i) != secondString.charAt(i)) 
        { 
         //Let's log the difference: 
         Log.d("app","Difference at char: "+i+" its value in the first string is: "+firstString.charAt(i)+" and its value in the first string is: "+secondString.charAt(i)); 
         //The strings are not equal 
         return false; 
        } 
       } 
       //All characters were equal 
       return true; 
      } 
} 

在這種情況下,你可以看到區別,如果有的話。

+0

謝謝。但我認爲使用等於和修剪要容易得多。 – behrooz

+0

沒問題,你可以把它作爲額外的答案:) – Mohammad