簡體   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