在我的案例中如何比较字符串 (android)
How to compare strings in my case (android)
我有两个字符串,我用这段代码得到它们
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"));
和LogCat输出是这样的
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给我看看这个
D/app﹕ is not premium
有什么问题吗?
PS:我试过了
1 . preLoad.equalsIgnoreCase(encrypted_id)
2 . preLoad.compareTo(encrypted_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 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;
}
}
在这种情况下,如果有差异,您可以看到。
我有两个字符串,我用这段代码得到它们
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"));
和LogCat输出是这样的
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给我看看这个
D/app﹕ is not premium
有什么问题吗?
PS:我试过了
1 . preLoad.equalsIgnoreCase(encrypted_id)
2 . preLoad.compareTo(encrypted_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 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;
}
}
在这种情况下,如果有差异,您可以看到。