要问这个问题可能会非常愚蠢,但我无法找到使其有效的方法,甚至无法在互联网上找到解决方案。
我需要连接2个字符串(变量STR_Prefix和字符串“TA +”)来创建一个新字符串(“ATA +”或“BTA +”),然后我必须检查if语句中的值,但它失败了, if语句没有检测到“ATA +”。
但如果我将“STR_Action”的值直接设置为“ATA +”,则效果非常好。
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();
}
}
请帮助:'(
提前致谢
答案 0 :(得分:3)
尝试使用.equals()
方法进行String
比较
if (STR_Action.equals("ATA+"))