如何在Java中替换不可打印的Unicode字符? [英] How can I replace non-printable Unicode characters in Java?

查看:515
本文介绍了如何在Java中替换不可打印的Unicode字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容将取代ASCII控制字符( [\ x00-\x1F \ x7F] 的简写):

The following will replace ASCII control characters (shorthand for [\x00-\x1F\x7F]):

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屋!

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