繁体   English   中英

需要删除字符串中多余的方括号

[英]Need to remove extra square brackets in the string

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);

OUTPUT:

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]

由于需要两个不同的替换。

使用以下代码

String s = "[[m String to be placedString not to be placed]]";
System.out.println(s.replaceAll("[[","[").replaceAll("]]","]");

如果您确定[[]始终位于确切位置,而]]始终位于结尾,则只需在同一SO答案线程中的其他答案中使用子字符串即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM