如何在 Java 中替换不可打印的 Unicode 字符? [英] How can I replace non-printable Unicode characters in Java?
本文介绍了如何在 Java 中替换不可打印的 Unicode 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下将替换 ASCII 控制字符([x00-x1Fx7F]
的简写):
The following will replace ASCII control characters (shorthand for [x00-x1Fx7F]
):
my_string.replaceAll("\p{Cntrl}", "?");
以下内容将替换所有 ASCII 不可打印字符([p{Graph}x20]
的简写),包括重音字符:
The following will replace all ASCII non-printable characters (shorthand for [p{Graph}x20]
), including accented characters:
my_string.replaceAll("[^\p{Print}]", "?");
然而,两者都不适用于 Unicode 字符串.有没有人有从 unicode 字符串中删除不可打印字符的好方法?
However, neither works for Unicode strings. Does anyone has a good way to remove non-printable characters from a unicode string?
推荐答案
my_string.replaceAll("\p{C}", "?");
查看有关 Unicode 正则表达式的更多信息.java.util.regexPattern
/String.replaceAll
支持它们.
See more about Unicode regex. java.util.regexPattern
/String.replaceAll
supports them.
这篇关于如何在 Java 中替换不可打印的 Unicode 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文