全部替换 ”?” 和 ”\\?” 在 Java 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13791495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace all "?" with "\\?" in Java
提问by GuiceU
is it possible to replace all the questionmarks ("?") with "\?" ?
是否可以将所有问号(“?”)替换为“\?” ?
Lets say I have a String, and I want to delete some parts of that String, one part with an URL in it. Like this:
假设我有一个字符串,我想删除该字符串的某些部分,其中一部分包含 URL。像这样:
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring.replaceAll(replacestring, "");
But! As I understand it you can't use the replaceAll() method with a String that contains one single questionmark, you have to make them like this "\?" first.
但!据我了解,您不能将 replaceAll() 方法与包含一个问号的字符串一起使用,您必须使它们像这样“\?” 第一的。
So the question is; Is there some way to replace questionmarks with "\?" in a String? And no, I'm able to just change the String.
所以问题是;有没有办法用“\”替换问号?在字符串中?不,我只能更改字符串。
Thanks in advance, hope someone understands me! (Sorry for bad English...)
在此先感谢,希望有人理解我!(抱歉英语不好...)
回答by Bohemian
Don't use replaceAll()
, use replace()
!
不要用replaceAll()
,用replace()
!
It is a common misconception that replaceAll()
replaces alloccurrences and replace()
just replaces one or something. This is totally incorrect.
这是一种常见的误解,它会replaceAll()
替换所有出现的事件,而replace()
只是替换一个或某事。这是完全不正确的。
replaceAll()
is poorly named - it actually replaces a regex.replace()
replaces simple Strings, which is what you want.
replaceAll()
名字不好 - 它实际上取代了regex。replace()
替换简单的字符串,这正是您想要的。
Both methods replace alloccurrences of the target.
这两种方法都会替换所有出现的目标。
Just do this:
只需这样做:
longstring = longstring.replace(replacestring, "");
And it will all work.
这一切都会奏效。
回答by PearsonArtPhoto
Escape the \
too, using \\\\?
.
逃离\
过,使用\\\\?
。
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring=longstring.replaceAll(replacestring, "\\?");
But as other answer have mentioned, replaceAll
is a bit overkill, just a replace
should work.
但正如其他答案所提到的,replaceAll
有点矫枉过正,只是replace
应该工作。
回答by arshajii
replaceAll
takes a regular expression, and ?
has a special meaning in the regex world.
replaceAll
采用正则表达式,?
在正则表达式世界中具有特殊意义。
You should use replace
in this case, since you don't need a regex.
您应该replace
在这种情况下使用,因为您不需要正则表达式。
String longstring = "..."; //This is the long String
String replacestring = "http://test.com/awesomeness.asp?page=27";
longstring = longstring.replace(replacestring, "");
Oh, and strings are immutable!! longstring = longstring.replace(..)
, notice the assignment.
哦,字符串是不可变的!!longstring = longstring.replace(..)
,注意分配。
回答by Eng.Fouad
Use String.replace()
instead of String.replaceAll()
:
使用String.replace()
代替String.replaceAll()
:
longstring = longstring.replace("?", "\?");
String.replaceAll()
uses Regular Expression, while String.replace()
uses plain text.
String.replaceAll()
使用正则表达式,而String.replace()
使用纯文本。