问题描述
我的问题与String类的replaceAll方法有关.
My question concerns the replaceAll method of String class.
我的目的是将文本中的所有破折号替换为基本的-".我知道em-dash的unicode字符是\ u2014.
My purpose is to replace all the em-dashes in a text with a basic "-".I know the unicode character of em-dash is \u2014.
我以以下方式尝试过:
String s = "asd – asd";
s = s.replaceAll("\u2014", "-");
仍然,不替换破折号.我在做什么错了?
Still, the em-dash is not replaced. What is it I'm doing wrong?
推荐答案
问题编辑后进行次要
您可能根本没有使用破折号.如果您不确定自己拥有什么,一个不错的解决方案是简单地找到并替换所有破折号... em或其他.看看 此答案 ,您可以尝试对所有的破折号使用 Unicode破折号标点属性 > \\ p {Pd}
You might not be using an em-dash at all. If you're not sure what you have, a nice solution is to simply find and replace all dashes... em or otherwise. Take a look at this answer, you can try to use the Unicode dash punctuation property for all dashes ==> \\p{Pd}
String s = "asd – asd";
s = s.replaceAll("\\p{Pd}", "-");
工作示例 用上述代码替换破折号和常规破折号.
Working example replacing an em dash and regular dash both with the above code.
参考文献:
公共字符串replaceAll(字符串正则表达式,字符串替换)
Unicode正则表达式
References:public String replaceAll(String regex, String replacement)
Unicode Regular Expressions
这篇关于正确的正则表达式,用于用基本的“-"代替破折号在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!