Remove list space

Asked 1 years ago, Updated 1 years ago, 105 views

How do I get rid of the blanks on the list? Spaces have become list components, so how do I get rid of them all?

Result value

['[', ', '\n', '\r', '', '\n', '\r', '', '', '\n', '\r', '', ', '\n', '\r', '', '', '\n', '\r', ']

What should I do if I want to get rid of all the gaps in the list?

list remove whitespace del

2022-09-22 19:46

1 Answers

You didn't write down your language, Typically, you can solve it using regular expressions.

//Java Example

List<String> list = new ArrayList<>();

list.add(" ");
list.add("\n");
list.add("\r");
list.add("\t");

System.out.println(list.size()); //Output: 4

list.removeIf(n->n.matches("\\s"); // Find blank characters such as tabs, line breaks, spaces, and so on.

System.out.println(list.size(); //output: 0


2022-09-22 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.