我是Java和Android的新手,所以我相信这将是一个简单的问题/答案。我知道要找出一个字符串是否等于另一个字符串,可以使用equal函数。在我的情况下,我正在扫描QR码,扫描结果类似于“雇员~~约翰·史密斯~~部门~~维护”。我需要知道如何执行以下操作:

String contents = intent.getStringExtra("SCAN_RESULT");
// I know that "contents" contains the string " EMPLOYEE~~John Smith~~DIVISION~~Maintenance"

String[] myJunk = contents.split("~~");
// This should split everything up into an array named myJunk (right)?

String val1 = myJunk[0];
// Now val1 Should be equal to "EMPLOYEE"

if (myJunk[0].equals(val1)){
    // Do Something
}


在示例Java代码中,myJunk [0]从不等于val1。我究竟做错了什么?

最佳答案

我已经尝试过了,并且可以工作,所以尝试显示content变量,可能问题出在附加功能中,尝试在logCat中显示它:

String contents = "EMPLOYEE~~John Smith~~DIVISION~~Maintenance";
 String[] myJunk = contents.split("~~");
 // This should split everything up into an array named myJunk (right)?

 String val1 = myJunk[0];
 Toast.makeText(this, "val1 = "+val1, Toast.LENGTH_LONG).show();
 Toast.makeText(this, "val2 = "+myJunk[1], Toast.LENGTH_LONG).show();
 // Now val1 Should be equal to "EMPLOYEE"

 if (myJunk[0].equals(val1)){
     Toast.makeText(this, "equals", Toast.LENGTH_LONG).show();
 }

10-06 00:40