我有QR码扫描结果,我想从结果中得到一个特定的字符串。
示例:如果结果是“ROGS,Hudson,J'kobi,Anderson,Harrison,454242,SAM,HARRY,TIM,JOHN,SAMMY,TONNY,SAMON,GROOD,TOD。”
我想从结果字符串中获取名称TONNY。在Android Java代码中,最好的方法是什么。
我的当前代码如下
public void handleResult(Result rawResult) {
// Do something with the result here
Log.e("handler", rawResult.getText()); // Prints scan results
Log.e("handler", rawResult.getBarcodeFormat().toString()); // Prints the scan format
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(rawResult.getText());
AlertDialog alert1 = builder.create();
alert1.show();
// If you would like to resume scanning, call this method below:
mScannerView.resumeCameraPreview(this);
}
答案 0 :(得分:0)
你不能做一个简单的字符串匹配来检查TONNY或任何其他名字吗?
String required = "TONNY";
String result = rawResult.getBarcodeFormat().toString();
return result.matches(required)
编辑:忽略大小写
result.toLowerCase().contains(required.toLowerCase())
编辑2:要获得编号,请拆分结果并循环
String array[] = result.split(",")
for(int i=0;i<array.length;i++)
if(array[i].equals(required))
return i