[英]Replace all the characters of “+” with empty character in string java
I have a string我有一个字符串
String myString = "Hello+World+How are you";
How to replace all "+"
in myString
with empty character
如何用
empty character
替换myString
中的所有"+"
I tried with : myString.replaceAll("+"," ");
我试过:
myString.replaceAll("+"," ");
What I am trying to find : Regular expression for +
so I can replace all occurrences我要查找的内容:
+
的正则表达式,因此我可以替换所有出现的地方
Output I am trying to achieve: Hello World How are you
Output 我正在努力实现:
Hello World How are you
try with like this像这样尝试
myString.replace("+"," ");
I usually use Pattern.quote(String)
我通常使用
Pattern.quote(String)
String myString = "Hello+World+How are you";
String replaced = myString.replaceAll(Pattern.quote("+"), " ");
I think it is less error-prone if you (or someone else) need to modify the regexp later.如果您(或其他人)以后需要修改正则表达式,我认为它不太容易出错。
You should use \\
with any special charachter in java,so your code will be like this to replace +
with whitespace:您应该在 java 中将
\\
与任何特殊字符一起使用,因此您的代码将像这样用空格替换+
:
myString.replaceAll("\\+"," ");
Explanation: \
this is used to escape special characters,and this \
itself is a special character,that is why we have to use '\\'.说明:
\
这是用来转义特殊字符的,而这个\
本身就是一个特殊字符,所以我们必须使用'\\'。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.