static ArrayList<String> coordinates = new ArrayList<String>();
static String str = "";
static ArrayList scribbles = new ArrayList();
coordinates.add("String to be placed, String not to be placed");
String codChange = coordinates.toString().replaceAll(", ", "");
StringBuffer sb = new StringBuffer(codChange);
sb.insert(1,"m ");
ArrayList aListNumbers = new ArrayList(Arrays.asList(sb.toString()));
System.out.println("Coordinates: " + aListNumbers.toString().replaceAll("\\[|\\]", ""));
scribbles.add(aListNumbers);
str = scribbles.toString();
System.out.println("String: " + str);
输出:
Coordinates: m String to be placedString not to be placed
String: [[m String to be placedString not to be placed]]
我希望字符串:带有单方括号,例如:
String: [m String to be placedString not to be placed]
最佳答案
因为需要两种不同的替换。
使用下面的代码
String s = "[[m String to be placedString not to be placed]]";
System.out.println(s.replaceAll("[[","[").replaceAll("]]","]");
如果您确定 [[ 的确切位置始终位于开头,而 ]] 位于末尾,则只需按照同一 SO 答案线程中其他答案中的建议使用子字符串即可。
关于java - 需要删除字符串中多余的方括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32915837/