OK, so I have an ArrayList that I need to return as a String. Right now I am using this approach:
List customers = new ArrayList<>();
L
I think the best solution to print list without brackets and without any separator( for java 8 and higher )
String.join("", YOUR_LIST);
You can alos you you own delimiter to separate printing elements.
String.join(", \n", YOUR_LIST);
example above separate each list element with comma and new line.
Java 8 version
List<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(4);
System.out.println(intList.stream().map(i -> i.toString()).collect(Collectors.joining(",")));
Output: 1,2,4
You could try to replace the '[' and ']' with empty space
String list = Arrays.toString(customers.toArray()).replace("[", "").replace("]", "");
You can use the method substring() to remove starting and ending brackets without tampering any entries in the ArrayList.
I used something like this to convert a ArrayList<String>
to String
String str = list.toString().substring(1, list.toString().length() - 1);