我收到此格式的字符串TEST [52.5, 77.5]
收到上面的字符串后,我想形成两个元素,如下所示
TEST 52.5
TEST 77.5
我已经尝试如下所示,但没有得到我想要的输出。
public class Test {
public static void main(String args[]) throws Exception {
String string = "TEST [52.5, 77.5]";
String strModf = string.replaceAll("\\[", " ");
String newStr = strModf.replaceAll("\\]", " ");
// Used StringBuffer here but not able to succeed .
}
}
最佳答案
我会做这样的事情:
String str = "TEST [52.5, 77.5]";
Pattern p = Pattern.compile("(.*?)\\s*\\[(.*)\\]");
Matcher m = p.matcher(str);
if (m.find()) {
String first = m.group(1);
String[] others = m.group(2).split("\\s*,\\s*");
for (String other : others)
System.out.println(first + " " + other);
}
TEST 52.5 TEST 77.5
关于java - 格式化字符串以从中形成一个新字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052529/