例如我有这样一个字符串:abc123[*]xyz[#]098[~]f9e
[*] 、 [#] 和 [~] 代表 3 个不同的不可打印字符。 我如何在 Java 中将它们替换为“X”?
弗兰克
最佳答案
我不确定我是否理解你的问题。如果您可以更好地表述它,我认为您可能只需要一个简单的正则表达式替换。
String r = s.replaceAll(REGEX, "X");
REGEX 取决于您的需要:
"\\*|#|~" : matches only '*', "#', and '~'
"[^\\d\\w]" : matches anything that is neither a digit nor a word character
"\\[.\\]" : matches '[' followed by ANY character followed by ']'
"(?<=\\[).(?=\\])" : matches only the character surrounded by '[' and ']'
关于java - 如何使用 Java 检测和替换字符串中的不可打印字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2485636/