用字符代码而不是regex替换字符串? [英] Replace a string by character code instead of regex?
问题描述
Java(或任何其他第三方库)是否提供了一个API,用于根据字符代码(在已知的 Charset
中)替换字符,而不是正则表达式?例如,要在给定字符串中用双引号替换双引号,可以使用:
Does Java (or any other 3rd party lib) provide an API for replacing characters based on character code (within a known Charset
of course) rather than a regex? For instance, to replace double quotes with single quotes in a given string, one might use:
String noDoubles = containsDoubles.replace("\"", "'");
quote是 U + 0022
。所以有什么东西可以搜索 U + 0022
字符的实例,单引号?
However the UTF-8 character code for a double quote is U+0022
. So is there anything that could search for instances of U+0022
characters and replace them with single quotes?
此外,不只是询问双/单引号,我在说的字符代码查找和替换任何2个字符。
Also, not just asking about double/single quotes here, I'm talking about the character code lookup and replacement with any 2 characters.
推荐答案
使用重载版本 - String#replace(char,char)
,因此,您可以使用它像这样:
Use the overloaded version - String#replace(char, char)
which takes characters. So, you can use it like this:
String str = "aa \" bb \"";
str = str.replace('\u0022', '\'');
System.out.println(str); // aa ' bb '
这篇关于用字符代码而不是regex替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!