如何在我的案例中比较字符串(android)
我有两个字符串,我用这个代码得到它们如何在我的案例中比较字符串(android),android,string,equals,Android,String,Equals,我有两个字符串,我用这个代码得到它们 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(
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"));
D/encrypted_id﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0=
D/preferences.getString﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0=
if(preLoad.equals(encrypted_id))
{
Log.d("app","is premium");
}
else {
Log.d("app","is not premium");
}
LogCat的输出是这样的
Log.d("encrypted_id",encrypted_id);
Log.d("preferences.getString",preferences.getString("pur", "no"));
D/encrypted_id﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0=
D/preferences.getString﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0=
if(preLoad.equals(encrypted_id))
{
Log.d("app","is premium");
}
else {
Log.d("app","is not premium");
}
所以我确保他们是平等的
现在我想这样比较它们
Log.d("encrypted_id",encrypted_id);
Log.d("preferences.getString",preferences.getString("pur", "no"));
D/encrypted_id﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0=
D/preferences.getString﹕ wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0=
if(preLoad.equals(encrypted_id))
{
Log.d("app","is premium");
}
else {
Log.d("app","is not premium");
}
但是给我看看这个
D/app﹕ is not premium
有什么问题吗
PS:我试过了
1。预加载.equalsIgnoreCase(加密的\u id)
2。预加载。比较(加密的\u id)==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 not premium");
}
else {
Log.d("app","is premium");
}
您可以尝试以下方法:
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;
}
}
public boolean字符串比较(String firstString,String secondString){
if(firstString.length()!=secondString.length()){
//这两个字符串的长度不相等
返回false;
}否则{
for(int i=0;i
在这种情况下,如果存在差异,您可以看到差异。您是否尝试过预加载.trim().equals(加密的\u id.trim())??是否可以在else中打印加密的\u id
和预加载
?使用equalignore大小写并使用trim(),然后检查如果比较预加载.equals,会发生什么(“wxgNDxqzNWNgYWrE0fxjaU07a54XBFnAToy56MAV1Y0=”)
和加密的_id.equals(“wxgndxqzngywre0fxjau07a54xbfNatoy56mav1y0=”)
@user3676184 equalignore对他的代码不是必需的,id必须是唯一的谢谢。但我认为使用equals和trim更容易。没问题,你可以把它作为额外的答案:)