I have a string arraylist that it prints this: [[aaa,bbb,..,fff]] I havent made it to figure out why the double brackets are happening, as if i have 2 arrays inside each other. So I decided to use regex. What should I use to remove the inside brackets?
-
How are you printing it? – mmmmmm Oct 04 '13 at 15:53
-
7Instead of blindly removing them, you should spend time figuring out why they're there. – arshajii Oct 04 '13 at 15:53
-
1How did you construct this `ArrayList
`? Maybe you *do* have 2 arrays inside each other. – dcsohl Oct 04 '13 at 16:07 -
2... or the first element is "[aaa", and the last is "fff]"... – ppeterka Oct 04 '13 at 16:10
-
http://stackoverflow.com/questions/19158655/converting-string-array-to-list-double-brackets I posted my problem here but none could help @arshajii – user2598911 Oct 04 '13 at 16:14
4 Answers
This will fix the problem:
String str = "[[aaa,bbb,..,fff]]";
str = str.replaceAll("\\[\\[", "[");
str = str.replaceAll("\\]\\]", "]");
However,
I havent made it to figure out why the double brackets are happening
That is something you should really investigate, rather than just fixing the symptom, find and fix the cause.

- 5,987
- 5
- 45
- 73
-
`String.replaceAll()` expects a regex as its first argument. I don't think this will work since `[` has a special meaning in regex. Try instead `str.replace("[[", "[")` since `replace()` expects a literal string for its first argument and not a regex. – dcsohl Oct 04 '13 at 16:06
-
http://stackoverflow.com/questions/19158655/converting-string-array-to-list-double-brackets – user2598911 Oct 04 '13 at 16:15
-
@dcsohl edited my answer. Just forgot to escape the `[` and `]` characters – StormeHawke Oct 04 '13 at 16:23
-
1@Cruncher, I was referring to this method: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29 - `String` is a subclass of `CharSequence`, so it works. Did you try it? – dcsohl Oct 04 '13 at 16:28
str.replaceAll("\\[(.*)\\]", "$1")

- 4,522
- 8
- 36
- 50
-
1Consider expanding your answer to explain to the asker _why_ this achieves the desired result, possibly linking to documentation. As is, this is only marginally useful – Joshua Dwire Oct 04 '13 at 16:19
-
It should most probably be `str.replaceAll("\\[(.*?)]", "$1")` – Wiktor Stribiżew Sep 15 '22 at 07:46
If the brackets are always on the end then
str = str.substring(1, str.length()-2);
would be the easiest. However, if this is part of a larger string, then regex like the other answers is best.
And, like others have told you, find the problem, don't just band-aid it.

- 7,641
- 1
- 31
- 65
-
http://stackoverflow.com/questions/19158655/converting-string-array-to-list-double-brackets – user2598911 Oct 04 '13 at 16:14
I would not rely on the toString()
method concatenating a String list... I'd much rather advise an utility method - if you don't want to use any external libraries.
public static String getFormattedString(Collection<String> input, String separator)
StringBuilder sb = new StringBuilder();
boolean first = true;
for(String s: input) {
if(!first) {
sb.append(separator);
} else {
first = false;
}
sb.append(s);
}
return sb.toString();
}
And in your code:
String result = getFormattedString(myStringList, ", ");
By doing this, it will be sure that you get the output you want, no matter what implementation you use...
If external libraries are OK, then you should not reinvent the wheel...
Apache Commons: StringUtil
String result = org.apache.commons.lang.StringUtils.join(myStringList, ", ");
Guava: Joiner
String result = com.google.common.base.Joiner.on(", ").join(myStringList);
(used info from this question, and its answers)