Java – how do I replace multiple items in a list?
•
Java
I added a few characters to the list to replace it with a string Here they are:
List<Character> list = new ArrayList<Character>(); list.add(','); list.add('?'); list.add(','); list.add(':'); list.add('-'); list.add('('); list.add(')'); list.add(';'); list.add('/');
I want to replace all the characters in the list with the string "s"
s.replaceAll(list,"");
Of course, I can't do this because list is not a string But what can I do?
Edit so if
String s = "I am cool; not good!";
I want the list to recognize that it contains'; ' And "!", It does not replace those characters in string s without anything The result will be:
"I am cool not good"
Solution
Instead of using list < character >, I would use regular expressions:
s.replaceAll("[,?:();/-]","");
If you absolutely must define characters in a list, first convert the list to such a regular expression
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码