I extract some Data into a Arraylist. And in every Item there are several line breaks (\n) that I want to get rid of afterwards.
I´ve tried to do this:
public List<String> MemberIDList() {
    // Getting the ArrayList
    idList =  listProjection.getIDListOfMembers();
    for (int i = 0; idList.size() > i; i++) {
       String item = idList.get(i);
        item.replaceAll("\n", "");
    }
    return idList;
}
If I print that out on the console, it still contains all the line breaks:
The ID ...... (not null) 
     145     
     145
Thanks
EDIT: I also want to filter unnecessary whitespace. Is ther a better option than running the answers down below twice. There are spaces --------------------- this huge.
idList.replaceAll(item -> item.replaceAll("\\s{2,}", "").trim());
I got it :D
Change
item.replaceAll("\n", "");
to
idList.set(i,item.replaceAll("\n", ""));
item.replaceAll doesn't modify the state of the String referenced by item (which is impossible, since String is immutable). It returns a new String instead.
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