在Java字符串中替换特殊字符

huangapple go评论64阅读模式
英文:

Replacing special characters in Java String

问题

我想要替换以下字符串中的所有特殊字符:

String a = "Test’‵";

我想要用破折号(-)替换 ’ 和 ‵。我已经尝试了以下方法:

a = a.replaceAll("’|‵", "-");

这会生成以下结果:

> Test------

而不是

> Test--

如何实现所需的结果?

英文:

I want to replace all special characters in the string shown below:

String a="Test’‵"

I want to replace ’ and ‵ with dashes (-). I have tried the following:

a=a.replaceAll("[’|‵]", "-");

This generates the following result:

> Test------

instead of

> Test--

How can I achieve the desired result?

答案1

得分: 1

不要使用方括号,因为它代表要匹配的一组单个字符(字符类)。

a = a.replaceAll(""’|‵"", "-");

演示!

英文:

Don't use square brackets, as it represents a set of single characters to match (a character class).

a=a.replaceAll("’|‵", "-");

Demo!

huangapple
  • 本文由 发表于 2020年7月22日 03:29:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63021784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定