【发布时间】:2015-02-25 08:35:13
【问题描述】:
可能被问到这会很愚蠢,但我找不到让它工作的方法,甚至在互联网上找到解决方案。
我需要连接 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();
}
}
请帮忙:'(
提前致谢
【问题讨论】:
标签: android string if-statement string-concatenation