Android-连接的字符串不是';通过if条件检测到t
也许被问到这个问题会很愚蠢,但我找不到让它工作的方法,甚至无法在互联网上找到解决方案 我需要连接两个字符串(变量STR_Prefix和字符串“TA+”)来创建一个新的字符串(“ATA+”或“BTA+”),然后我必须检查if语句中的值,但它失败了,if语句没有检测到“ATA+” 但是,如果我将“STR_Action”的值直接设置为“ATA+”,它将非常有效Android-连接的字符串不是';通过if条件检测到t,android,string,if-statement,string-concatenation,Android,String,If Statement,String Concatenation,也许被问到这个问题会很愚蠢,但我找不到让它工作的方法,甚至无法在互联网上找到解决方案 我需要连接两个字符串(变量STR_Prefix和字符串“TA+”)来创建一个新的字符串(“ATA+”或“BTA+”),然后我必须检查if语句中的值,但它失败了,if语句没有检测到“ATA+” 但是,如果我将“STR_Action”的值直接设置为“ATA+”,它将非常有效 public void Test(boolean ok) { String STR_Action = ""; if (ok)
public void Test(boolean ok)
{
String STR_Action = "";
if (ok) { STR_Prefix = "A"; }
else { STR_Prefix = "B"; }
/* I have tried some ways to concatenate */
STR_Action = STR_Prefix + "TA+"; // Not working with the if-statement
// STR_Action = new StringBuilder(STR_Prefix).append("TA+").toString(); // Not working with the if-statement
/* But if-statement only works when I set STR_Action directly with = "ATA+" or "BTA+"; */
// STR_Action = "ATA+"; // This assign is detected by the if-statement
if (STR_Action == "ATA+")
{
Toast.makeText(getApplicationContext(), "ATA+", 0).show();
}
else if (STR_Action == "BTA+")
{
Toast.makeText(getApplicationContext(), "ATA-", 0).show();
}
else
{
Toast.makeText(getApplicationContext(), "Code not found", 0).show();
}
}
求求你,救命(
提前感谢尝试使用
.equals()
方法进行字符串
比较
if (STR_Action.equals("ATA+"))
谢谢M D,它工作得很好。但我仍然不明白:STR_Action=STR_Prefix+“TA+”;STR_Action=“ATA+”;两者都意味着STR_Action=“ATA+”,但在使用“==”:-串联:失败-直接分配:有效时,“if”对两者都不起作用