英文:
Replace non-UTF-8 character from String XML by space in java
问题
Checking this post, I'm able to replace the ’
character which is the apostrophe character ’
from my String XML by space:
String s = "<content>abc’s house.</content>";
s = s.replaceAll("[^\\x00-\\x7F]", " ");
System.out.println(s);
The issue is that it produces 3 spaces: abc s house.
, I'd say because of ’
having 3 characters maybe? But I need for that character to be replaced by just one space: abc s house.
If I use below approach, it works while running from eclipse, but when I compile it to an executable jar, then it converts the ’
by the ’
and since the string doesn't have the ’
it doesn't work. (I'm able to see this behavior by decompiling the jar and see the code):
s = s.replace("’", " ");
英文:
Checking this post, I'm able to replace the ’
character which is the apostrophe character ’
from my String XML by space:
String s = "<content>abc’s house.</content>";
s = s.replaceAll("[^\\x00-\\x7F]"," ");
System.out.println(s);
The issue is that it produces 3 spaces: abc s house.
, I'd say because of ’
having 3 characters maybe? But I need for that character to be replaced by just one space: abc s house.
If I use below approach, it works while running from eclipse, but when I compile it to an executable jar, then it converts the ’
by the ’
and since the string doesn't have the ’
it doesn't work. (I'm able to see this behavior by decompiling the jar and see the code):
s = s.replace("’", " ");
答案1
得分: 1
你可以使用 +
量词:
String s = "abc’s house.";
s = s.replaceAll("[^\\x00-\\x7F]+"," ");
System.out.println(s);
输出结果:
abc s house.
英文:
You can use +
quantifier:
String s = "abc’s house.";
s = s.replaceAll("[^\\x00-\\x7F]+"," ");
System.out.println(s);
prints:
> abc s house.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论