Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing [ and ] from arraylist

Tags:

java

I have to print the list values in the form of String. But I am held up with the [ and ] in the list. Here is my code.

List dbid=new ArrayList();
dbid.add(ar.getdbID());
String check=ar.getdbID().toString();

output for the above code :

[2, 3,4]

But I just need this:

2,3,4
like image 227
sahana Avatar asked Apr 17 '26 05:04

sahana


1 Answers

There are no [ and ] "in the List". It's only the String representation (produced by toString()) that contains those characters. It's important to distinguish those two things.

I'd use a Guava Joiner:

Joiner.on(',').join(dbid);

Of you can manually implement it:

StringBuilder b = new StringBuilder();
Iterator<?> it = dbid.iterator();
while (it.hasNext()) {
  b.append(it.next());
  if (it.hasNext()) {
    b.append(',');
  }
}
String result = b.toString();
like image 156
Joachim Sauer Avatar answered Apr 18 '26 19:04

Joachim Sauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!