Efficiently removing specific characters (some punctuation) from Strings in Java?

后端 未结 7 687
青春惊慌失措
青春惊慌失措 2020-12-10 17:14

In Java, what is the most efficient way of removing given characters from a String? Currently, I have this code:

private static String processWord(String x)          


        
7条回答
  •  渐次进展
    2020-12-10 17:38

    Use String#replaceAll(String regex, String replacement) as

    tmp = tmp.replaceAll("[,.;!?(){}\\[\\]<>%]", "");
    
    System.out.println(
       "f,i.l;t!e?r(e)d {s}t[r]ig%".replaceAll(
                       "[,.;!?(){}\\[\\]<>%]", "")); // prints "filtered string"
    

提交回复
热议问题