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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With