How to remove control characters from java string?

前端 未结 7 2119
没有蜡笔的小新
没有蜡笔的小新 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 17:11

    This seems to be an option

        String s = "\u0001\t\r\n".replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "");
        for (char c : s.toCharArray()) {
            System.out.print((int) c + " ");
        }
    

    prints 9 13 10 just like you said "except carriage returns, line feeds, and tabs".

    0 讨论(0)
提交回复
热议问题