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:以单个方括号显示,如:
String: [m String to be placedString not to be placed]
答案 0 :(得分:1)
由于需要两种不同的替代品。
使用以下代码
String s = "[[m String to be placedString not to be placed]]";
System.out.println(s.replaceAll("[[","[").replaceAll("]]","]");
如果你确定[[在开头和]]的确切位置是在结尾,只需使用同一个SO答案线程中另一个答案中建议的子字符串。