Replace the brackets and commas with empty space.
String formattedString = myArrayList.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim(); //remove trailing spaces from partially initialized arrays
Answer from user489041 on Stack OverflowReplace the brackets and commas with empty space.
String formattedString = myArrayList.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim(); //remove trailing spaces from partially initialized arrays
Basically, don't use ArrayList.toString() - build the string up for yourself. For example:
StringBuilder builder = new StringBuilder();
for (String value : publicArray) {
builder.append(value);
}
String text = builder.toString();
(Personally I wouldn't call the variable publicArray when it's not actually an array, by the way.)
You could try to replace the '[' and ']' with empty space
String list = Arrays.toString(customers.toArray()).replace("[", "").replace("]", "");
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 also add your own delimiter to separate printing elements.
String.join(", \n", YOUR_LIST);
example above separate each list element with comma and new line.
You can use replaceAll and simply:
myStr = myStr.replaceAll("\\[|\\]", "");
Try this
StringBuilder builder = new StringBuilder();
for (String value : publicArray) {
builder.append(value +",");
// or builder.append(value).append(",");
}
String text = builder.toString();
System.out.print(text);
you need a second for loop:
for (int i = 0; i < combine.length; i++) {
if (combine[i].size() > 0) {
for (Object obj : combine[i]) {
System.out.println(obj);
}
}
}
This will print:
X
XXX
XXXXX
XXXXXXX
XXXXXXXXX
#
###
#####
#######
#########
Seems like your square brackets [] are from empty Stacks.
So in your for loop make sure not to print empty stacks.
To avoid the commas, don't print the stack with its automatic toString(), but iterate over it yourself.
Edit: Solved! Thank you dartalley!
Made a post earlier about a program checking the validity of passwords written in textfields or read from files and this is the continuation, sorta. (Thanks a ton to those who helped, btw!) I have to return the ArrayList of passwords that did not meet the requirements, but I can't remove the brackets/commas:
[334455 The password must contain at least one alphabetic character. , george The password must contain at least one digit. , 4sale The password must be at least 6 characters long. , abcdef The password must contain at least one digit. , apples The password must contain at least one digit. , aa11b The password must be at least 6 characters long. , pilotproject The password must contain at least one digit. , mypassword The password must contain at least one digit. ]
I can't manipulate it and return it as an ArrayList. If I do, it'll call the toString method of the ArrayList class and return them all anyways! I tried StringBuilder and StringTokenizer as well as setting delimiters to filter out the things I don't need, but those work for Strings, not an ArrayList of them.
I'm thinking I need to re-define the definition of the toString method within the ArrayList class, but that seems too complicated. I have to be missing something. Any help is appreciated. Thanks!
You are probably calling System.out.println to print the list. The JavaDoc says:
This method calls at first String.valueOf(x) to get the printed object's string value
The brackets are added by the toString implementation of ArrayList. To remove them, you have to first get the String:
String errorDisplay = errors.toString();
and then strip the brackets, something like this:
errorDisplay = errorDisplay.substring(1, errorDisplay.length() - 1);
It is not good practice to depend on a toString() implementation. toString() is intended only to generate a human readable representation for logging or debugging purposes. So it is better to build the String yourself whilst iterating:
List<Integer> errors = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int x = 1; x<10; x++) {
errors.add(x);
sb.append(x).append(",");
}
sb.setLength(sb.length() - 1);
String errorDisplay = sb.toString();
Note that this is not an array, just a String displaying the contents of the list. To create an array from a list you can use list.toArray():
// create a new array with the same size as the list
Integer[] errorsArray = new Integer[errors.size()];
// fill the array
errors.toArray(errorsArray );
EDIT: From an object-oriented perspective one could argue that errors and errorsDisplay conceptually belong together in a class, e.g:
public class Errors {
private final List<Integer> codes = new ArrayList<>();
public void add(int error) {
codes.add(error);
}
public Stream<Integer> get() {
return codes.stream();
}
@Override
public String toString() {
return codes.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
}
}
Short answer: System.out.println(errors.toString().substring(1, errors.toString().length() - 1))
Explanation: when you call System.out.println(obj) with an Object as a parameter, the printed text will be the result of obj.toString(). ArrayList.toString() is implemented in a way that makes it represent its content between brackets [] in a comma separated concatenation of each of the contained items (their .toString() representation as well).
It is not a good practice to rely on another class's toString() implementation. You should use your own way to format your result.