需要删除字符串中多余的方括号
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);
输出:
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 答案线程中其他答案中的建议使用子字符串即可。
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 答案线程中其他答案中的建议使用子字符串即可。