OK, so I have an ArrayList that I need to return as a String. Right now I am using this approach:
List<Customer> customers = new ArrayList<>(); List<Account> accounts = new ArrayList<>(); public String customerList() { String list = Arrays.toString(customers.toArray()); return list; } public String accountList() { String account = Arrays.toString(accounts.toArray()); return account; }
This works fine, but the problem is that I get brackets around the text. I am getting stuff like:
Customer list: -------------- [Name: Last, First , Name: Last, First , Name: Last, First ]
When I want something like this:
Customer list: -------------- Name: Last, First Name: Last, First Name: Last, First
However, unlike similar questions, I don't simply want to output line by line. I need to store the ArrayList in a String without the brackets so I can work with it.
EDIT: It should be noted that the one comma in the version I want it to look like was placed there by a method in a different class:
public final String getName() { return getLast() + ", " + getFirst(); }
EDIT 2: I was able to find a complete solution by adapting the two answers I was given. It was difficult deciding which was the "answer" because I found both useful, but went with the one that I could use more selectively.
public String customerList() { String list = Arrays.toString(customers.toArray()).replace(", N", "N").replace(", N", "N"); return list.substring(1,list.length()-1); }
To remove the brackets I used the modified return. Removing the comma required me to focus on the fact that each line will start with an "N", but the flaw in this approach is that the code would break if I forget to change it here if I change it there. Still, it solves my specific problem, and I can always notate to myself in both places as needed.
To print a NumPy array without enclosing square brackets, the most Pythonic way is to unpack all array values into the print() function and use the sep=', ' argument to separate the array elements with a comma and a space.
To convert the contents of an ArrayList to a String, create a StringBuffer object append the contents of the ArrayList to it, finally convert the StringBuffer object to String using the toString() method.
Or do you override the toString method to return the string as per you wanted. 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);
We can convert array to string by iterating array and capturing each element from an array and append to StringBuilder/StringBuffer so that final output will be string without brackets. Lets see an example java program to convert array to string by removing brackets.
How to Print Arraylist Without Brackets Java | In this case, we print the array list without brackets, usually when we use ArrayList the elements are printed within the brackets. To do this we can use replace method which replaces the brackets by space. Now see some below examples to get to the know the problem clearly,
If you take a look inside Arrays.toString you'll see that it's implemented by appending elements to StringBuilder. I suggest you to copy code from there and remove lines that are responsible for appending commas and brackets. String list = Arrays.toString (customers.toArray ()).replace (" [", "").replace ("]", "");
You could try to replace the '[' and ']' with empty space
String list = Arrays.toString(customers.toArray()).replace("[", "").replace("]", "");
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
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