-3

Replacing square brackets is not a problem, but the problem is with replacing commas, replace(",", "") , because it does it with all commas in my table and i need to delete only those which appears because of Arrays.toString()

System.out.println( Arrays.toString( product ).replace("[", "").replace("]", "").replace(",", ""));

If there is no way of do that, maybe there are other ways to print my array, like string builder...etc? but i am not sure how to use it

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

2

Rather than using Arrays.toString, since you don't want the output it creates, use your own loop:

StringBuilder sb = new StringBuilder(400);
for (int i = 0; i < product.length; ++i) {
    sb.append(product[i].toString());
}
String result = sb.toString();

Note I'm using toString on your product entries; there may be a more appropriate choice depending on what those are.

If you want a delimiter (other than ,, obviously), just append it to the StringBuilder as you go:

StringBuilder sb = new StringBuilder(400);
for (int i = 0; i < product.length; ++i) {
    if (i > 0) {
        sb.append(yourDelimiter);
    }
    sb.append(product[i].toString());
}
String result = sb.toString();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

We dont know what your objects look like in your array, but you shouldnt use Arrays.toString if you dont like the output since its only a helper method to save you some time. Just iterate over your objects with a loop and print them.

Ben
  • 1,157
  • 6
  • 11
0

There's a great library of apache where you can achieve your goal in one line of code: org.apache.commons.lang.StringUtils

String delimiter = " ";
StringUtils.join(array, delimiter);
Lital Kolog
  • 1,301
  • 14
  • 39