如何从java字符串中删除控制字符? [英] How to remove control characters from java string?

查看:1145
本文介绍了如何从java字符串中删除控制字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自UI的字符串可能包含控制字符,我想删除除回车换行标签之外的所有控制字符

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, and tabs.

现在我可以找到两种方法删除所有控制字符:

Right now I can find two way to remove all control characters:

1-使用guava:

return CharMatcher.JAVA_ISO_CONTROL.removeFrom(string);

2-使用正则表达式:

return string.replaceAll("\\p{Cntrl}", "");


推荐答案

如果要删除,可以执行以下操作其他或控制单码类别中的所有字符

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

注意:这实际上删除了(除其他外)'\\ \\ u008f'来自字符串的Unicode字符,而不是转义形式的%8F字符串。

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

礼貌: polygenelubricants 替换Unicode控制字符

这篇关于如何从java字符串中删除控制字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆