How to remove control characters from java string?

前端 未结 7 2117
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 16:41

I have a string coming from UI that may contains control characters, and I want to remove all control characters except carriage returns, line feeds

7条回答
  •  有刺的猬
    2020-12-15 16:55

    You can do something like this if you want to delete all characters in other or control uni-code category

    System.out.println(
        "a\u0000b\u0007c\u008fd".replaceAll("\\p{Cc}", "")
    ); // abcd
    

    Note : This actually removes (among others) '\u008f' Unicode character from the string, not the escaped form "%8F" string.

    Courtesy : polygenelubricants ( Replace Unicode Control Characters )

提交回复
热议问题