Remove non-ASCII non-printable characters from a String

后端 未结 6 2297
轻奢々
轻奢々 2020-12-02 13:56

I get user input including non-ASCII characters and non-printable characters, such as

\\xc2d
\\xa0
\\xe7
\\xc3\\ufffdd
\\xc3\\ufffdd
\\xc2\\xa0
\\xc3\\xa7
         


        
6条回答
  •  我在风中等你
    2020-12-02 14:24

    I know it's maybe late but for future reference:

    String clean = str.replaceAll("\\P{Print}", "");
    

    Removes all non printable characters, but that includes \n (line feed), \t(tab) and \r(carriage return), and sometimes you want to keep those characters.

    For that problem use inverted logic:

    String clean = str.replaceAll("[^\\n\\r\\t\\p{Print}]", "");
    

提交回复
热议问题