I'm making a medicine expiry date reminder project by using zxing library. I'm taking the qr code, trying to get expiry date after space character and transform it to date format. e.g. qr code = "010869971701010921104197131 1722022810170666" > (17)220228 > yyMMdd to ddMMyyyy > result: 28.02.2022
public void handleResult(Result result) {
String resultCode = result.getText();
String[] date1 = resultCode.split("\\s");
String s = date1[1].substring(2,8);
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
try {
Date d1 = sdf.parse(s);;
sdf.applyPattern("dd.MM.yyyy");
MainActivity.date.setText(sdf.format(d1));
}catch(ParseException e) {
e.printStackTrace();
}
App is getting crash with this code. If I put value to resultCode manually, app is working. e.g. resultCode = "010869971701010921104197131 1722022810170666" > 28.02.2022
I noticed that the problem is in this line: String[] date1 = resultCode.split("\s");
Then I tried to replace all space character([android splitting with space not working for this case. Why?):
resultCode = resultCode.replaceAll("\\t", "a");
resultCode = resultCode.replaceAll("\\xA0", "b");
resultCode = resultCode.replaceAll("\\u1680", "c");
resultCode = resultCode.replaceAll("\\u180e", "d");
resultCode = resultCode.replaceAll("\\u2000", "e");
resultCode = resultCode.replaceAll("\\u200a", "f");
resultCode = resultCode.replaceAll("\\u202f", "g");
resultCode = resultCode.replaceAll("\\u205f", "h");
resultCode = resultCode.replaceAll("\\u3000", "i");
>>>resultCode = resultCode.replaceAll("(^\\h*)|(\\h*$)","j");
resultCode = resultCode.replaceAll("\\u00A0","k");
resultCode = resultCode.replaceAll("\\u2007","l");
result: "j010869971701010921104197131 1722022810170666j"
Only worked the line that I marked with ">>>". Isn't that a space character? What may be the problem? How can I get expiry date after the blank and "17"?
Although I defined resultCode as a string, its type could be different?
String resultCode = result.getText();
resultCode is getting the data directly from QR code scanner.