[英]Replacing \\u by \u in java string
I have a string which contains normal text and Unicode in between, for example "abc\abc". 我有一个包含普通文本和Unicode之间的字符串,例如“ abc \\ ue415abc”。 I want to replace all occurrences of
\\\\u\u003c/code> with
\\u\u003c/code> .
我想用
\\u\u003c/code>替换所有出现的
\\\\u\u003c/code>
\\u\u003c/code> 。
How can I achieve this?
我该如何实现? I used the following code but it's not working properly.
我使用了以下代码,但无法正常工作。
String s = "aaa\\u2022bbb\\u2014ccc";
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile("\\\\u([0-9A-Fa-f]{4})").matcher(s);
while (m.find()) {
try {
int cp = Integer.parseInt(m.group(1), 16);
m.appendReplacement(buf, "");
buf.appendCodePoint(cp);
} catch (NumberFormatException e) {
}
}
m.appendTail(buf);
s = buf.toString();
Please help.
请帮忙。 Thanks in advance.
提前致谢。
From API reference: http://developer.android.com/reference/java/lang/String.html#replace(java.lang.CharSequence , java.lang.CharSequence) 从API参考: http : //developer.android.com/reference/java/lang/String.html#replace (java.lang.CharSequence,java.lang.CharSequence)
You can use public 您可以使用公开
public String replace (CharSequence target, CharSequence replacement)
string = string.replace("\\u", "\u");
or
String replacedString = string.replace("\\u", "\u");
尝试这个:
s = s.replace(s.indexOf("\\u"), "\u");
There is a contains
method and a replace
method in String. String中
contains
一个contains
方法和一个replace
方法。 That being said 话虽如此
String hello = "hgjgu\udfgyud\\ushddsjn\hsdfds\\ubjn";
if(hello.contains("\\u"))
hello.replace("\\u","\u");
System.out.println(hello);
It will print :- hgjgu\\udfgyud\\ushddsjn\\hsdfds\\ubjn\u003c/strong> 它将打印: -hgjgu \\ udfgyud \\ ushddsjn \\ hsdfds \\ ubjn
Your initial string doesn't, in fact, have any double backslashes. 实际上,您的初始字符串没有任何双反斜杠。
String s = "aaa\\u2022bbb\\u2014ccc";
yields a string that contains aaa\•bbb\—ccc
, as \\\\
is just java string-literal escaping for \\
. 产生一个包含
aaa\•bbb\—ccc
的字符串,因为\\\\
只是Java字符串字面量对\\
转义。
If you want unicode characters: (StackOverflow21028089.java) 如果您想要Unicode字符:(StackOverflow21028089.java)
import java.util.regex.*;
class StackOverflow21028089 {
public static void main(String[] args) {
String s = "aaa\\u2022bbb\\u2014ccc";
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile("\\\\u([0-9A-Fa-f]{4})").matcher(s);
while (m.find()) {
try {
// see example:
// http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#appendReplacement%28java.lang.StringBuffer,%20java.lang.String%29
int cp = Integer.parseInt(m.group(1), 16);
char[] chars = Character.toChars(cp);
String rep = new String(chars);
System.err.printf("Found %d which means '%s'\n", cp, rep);
m.appendReplacement(buf, rep);
} catch (NumberFormatException e) {
System.err.println("Confused: " + e);
}
}
m.appendTail(buf);
s = buf.toString();
System.out.println(s);
}
}
=> =>
Found 8226 which means '•'
Found 8212 which means '—'
aaa•bbb—ccc
If you want aaa\•bbb\—ccc
, that's what you started with. 如果您想要
aaa\•bbb\—ccc
,那就是您的开始。 If you meant to start with a string literal with aaa\\\•bbb\\\—ccc
, that's this: 如果您要以
aaa\\\•bbb\\\—ccc
字符串文字aaa\\\•bbb\\\—ccc
,那就是:
String s = "aaa\\\\u2022bbb\\\\u2014ccc";
and converting it to the one with single slashes can be as simple as @Overv's code: 并将其转换为一个单斜杠就可以像@Overv的代码一样简单:
s = s.replaceAll("\\\\u", "\\u");
though since backslash has a special meaning in regex patterns and replacements (see Matcher's docs ) (in addition to java parsing), this should probably be: 尽管由于反斜杠在正则表达式模式和替换中具有特殊含义(请参阅Matcher的docs )(除了Java解析),它可能应该是:
s = s.replaceAll("\\\\\\\\u", "\\\\u");
=> =>
aaa\u2022bbb\u2014ccc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.