提问人:Somebody 提问时间:6/29/2023 最后编辑:gdomoSomebody 更新时间:6/29/2023 访问量:162
在 java 中将字符串 XML 中的非 UTF-8 字符替换为空格
Replace non-UTF-8 character from String XML by space in java
问:
检查这篇文章,我能够用空格替换我的字符串 XML 中的撇号字符:’
’
String s = "<content>abc’s house.</content>";
s = s.replaceAll("[^\\x00-\\x7F]"," ");
System.out.println(s);
问题是它产生了 3 个空格:,我会说因为有 3 个字符可能?但我需要将该字符替换为一个空格:abc s house.
’
abc s house.
如果我使用以下方法,它在从 eclipse 运行时可以工作,但是当我将其编译为可执行 jar 时,它会将 by 和 转换,因为字符串没有 它不起作用。(我能够通过反编译 jar 并查看代码来查看此行为):’
’
’
s = s.replace("’", " ");
答:
1赞
gdomo
6/29/2023
#1
您可以使用量词:+
String s = "abc’s house.";
s = s.replaceAll("[^\\x00-\\x7F]+"," ");
System.out.println(s);
指纹:
ABC的房子。
评论
replaceAll("[^\\x00-\\x7F]+"," ")
?!(只是添加了一个以匹配一次或多次)||顺便说一句,请注意,这更像是非 ASCII 而不是非 UTF-8+
^\x00-\x7F