无法使用 android 中的前缀和后缀从传入消息中提取字符串
Cannot extract a string from incoming message using prefix and suffix in android
我收到两种格式略有不同的传入消息,例如:
- "Info : STD ID2.7384733928374. PSSID, SSN:324920349023742903, END"
- "Info : STD Z10 1234567890123 PSSID, SSN:12394847382940398433, END"
我可以使用以下方法从第一种格式中成功提取字符串:
String message = "Info : STD ID2.7384733928374. PSSID, SSN:324920349023742903, END";
String prefix1 = "ID";
String suffix1 = ".";
String output1 = message.substring(message.indexOf(prefix1) + prefix1.length(), message.indexOf(suffix1));
Log.d("Output1", output1);
String prefix2 = ".";
String suffix2 = ". PSSID";
String output2 = message.substring(message.indexOf(prefix2) + prefix2.length(), message.indexOf(suffix2));
Log.d("Output2", output2);
// Output2: 7384733928374 (correct)
- 输出 1:2(正确)和
- 输出 2:7384733928374(正确)
但无法使用与上述相同的方法从第二种格式中提取:
String message = "Info : STD Z10 1234567890123 PSSID, SSN:12394847382940398433, END";
String prefix1 = "Z";
String suffix1 = " ";
String output1 = message.substring(message.indexOf(prefix1) + prefix1.length(), message.indexOf(suffix1));
Log.d("Output1", output1);
String prefix2 = " ";
String suffix2 = " PSSID";
String output2 = message.substring(message.indexOf(prefix2) + prefix2.length(), message.indexOf(suffix2));
Log.d("Output2", output2);
- 输出 1:错误 java.lang.StringIndexOutOfBoundsException:长度=65;区域开始=12;区域长度=-8
- Output2 STD Z10 1234567890123(错误)正确的输出应该是1234567890123
请问如何从第二种格式中提取10和1234567890123?
这是解决方案,它一定会给您带来预期的答案。这个问题我是用不同的工具完成的,很抱歉方法语法不同,但希望答案是正确的。
String message = "Info : STD Z10 1234567890123 PSSID, SSN:12394847382940398433, END";
String prefix1 = "Z";
String suffix1 = "0 ";
String output1 = message.Substring(message.IndexOf(prefix1) + prefix1.Length, message.IndexOf(suffix1)-11);
String prefix2 = "Z";
String suffix2 = " PSSID";
String output2 = message.Substring(message.IndexOf(prefix2) + prefix2.Length+2, message.IndexOf(suffix2)-14);
您可以使用正则表达式来匹配这两种情况:
Pattern pattern = Pattern.compile("\d+");
Matcher matcher = pattern.matcher(message);
matcher.find();
System.out.println(matcher.group());
matcher.find(matcher.end());
System.out.println(matcher.group());
我收到两种格式略有不同的传入消息,例如:
- "Info : STD ID2.7384733928374. PSSID, SSN:324920349023742903, END"
- "Info : STD Z10 1234567890123 PSSID, SSN:12394847382940398433, END"
我可以使用以下方法从第一种格式中成功提取字符串:
String message = "Info : STD ID2.7384733928374. PSSID, SSN:324920349023742903, END";
String prefix1 = "ID";
String suffix1 = ".";
String output1 = message.substring(message.indexOf(prefix1) + prefix1.length(), message.indexOf(suffix1));
Log.d("Output1", output1);
String prefix2 = ".";
String suffix2 = ". PSSID";
String output2 = message.substring(message.indexOf(prefix2) + prefix2.length(), message.indexOf(suffix2));
Log.d("Output2", output2);
// Output2: 7384733928374 (correct)
- 输出 1:2(正确)和
- 输出 2:7384733928374(正确)
但无法使用与上述相同的方法从第二种格式中提取:
String message = "Info : STD Z10 1234567890123 PSSID, SSN:12394847382940398433, END";
String prefix1 = "Z";
String suffix1 = " ";
String output1 = message.substring(message.indexOf(prefix1) + prefix1.length(), message.indexOf(suffix1));
Log.d("Output1", output1);
String prefix2 = " ";
String suffix2 = " PSSID";
String output2 = message.substring(message.indexOf(prefix2) + prefix2.length(), message.indexOf(suffix2));
Log.d("Output2", output2);
- 输出 1:错误 java.lang.StringIndexOutOfBoundsException:长度=65;区域开始=12;区域长度=-8
- Output2 STD Z10 1234567890123(错误)正确的输出应该是1234567890123
请问如何从第二种格式中提取10和1234567890123?
这是解决方案,它一定会给您带来预期的答案。这个问题我是用不同的工具完成的,很抱歉方法语法不同,但希望答案是正确的。
String message = "Info : STD Z10 1234567890123 PSSID, SSN:12394847382940398433, END";
String prefix1 = "Z";
String suffix1 = "0 ";
String output1 = message.Substring(message.IndexOf(prefix1) + prefix1.Length, message.IndexOf(suffix1)-11);
String prefix2 = "Z";
String suffix2 = " PSSID";
String output2 = message.Substring(message.IndexOf(prefix2) + prefix2.Length+2, message.IndexOf(suffix2)-14);
您可以使用正则表达式来匹配这两种情况:
Pattern pattern = Pattern.compile("\d+");
Matcher matcher = pattern.matcher(message);
matcher.find();
System.out.println(matcher.group());
matcher.find(matcher.end());
System.out.println(matcher.group());