keep only alphabet characters

后端 未结 5 2014
离开以前
离开以前 2021-02-06 22:34

What method should I follow in java to produce

\"WordWord\"

from

\"Word#$#$% Word 1234\"
5条回答
  •  萌比男神i
    2021-02-06 23:30

    You can use String.replaceAll(regex, replacement) with the regex [^A-Za-z]+ like this:

    String newstr = "Word#$#$% Word 1234".replaceAll("[^A-Za-z]+", "");
    // newstr will become WordWord
    

    Edit: Although OP hasn't mentioned anything about unicode characters but since @Joey has made a comment and if at all there a requirement to keep unicode characters then \\P{L}+ regex should be used like this:

    String newstr = "Word#$#$% Word λ1234ä, ñ, ж".replaceAll("\\P{L}+", "");
    // newstr will become WordWordλäñж
    

提交回复
热议问题