简体   繁体   English

用字符代码代替正则表达式替换字符串?

[英]Replace a string by character code instead of regex?

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? Java(或任何其他第三方库)是否提供了用于基于字符代码(当然在已知Charset内)而不是基于正则表达式的Charset替换的API? For instance, to replace double quotes with single quotes in a given string, one might use: 例如,要将给定字符串中的双引号替换为单引号,可以使用:

String noDoubles = containsDoubles.replace("\"", "'");

However the UTF-8 character code for a double quote is U+0022 . 但是,双引号的UTF-8字符代码是U+0022 So is there anything that could search for instances of U+0022 characters and replace them with single quotes? 那么,有什么可以搜索U+0022字符实例并将其替换为单引号的东西吗?

Also, not just asking about double/single quotes here, I'm talking about the character code lookup and replacement with any 2 characters. 另外,我不仅在这里询问双引号或单引号,我还说的是字符代码查找和任意2个字符的替换。

Use the overloaded version - String#replace(char, char) which takes characters. 使用重载版本String#replace(char, char)接受字符。 So, you can use it like this: 因此,您可以像这样使用它:

String str = "aa \" bb \"";
str = str.replace('\u0022', '\'');
System.out.println(str);  // aa ' bb '

Simply use the unicode literal: 只需使用unicode文字即可:

// I'm using an unicode literal for "
String noDoubles = containsDoubles.replace('\u0022', '\'');

The above will work for any character, as long as you know its corresponding code. 只要您知道相应的代码,以上内容就可以用于任何字符。

You can also use a regex still. 您还可以使用正则表达式。 From the Javadoc : Javadoc

\\xhh The character with hexadecimal value 0xhh \\ xhh十六进制值为0xhh的字符

\\uhhhh The character with hexadecimal value 0xhhhh \\ uhhhh十六进制值为0xhhhh的字符

Hence you could write this: 因此,您可以这样写:

String noDoubles = containsDoubles.replace("\\u0022", "'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM