Java,删除字符串对象中的反斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5104192/
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
Java, Removing backslash in string object
提问by Kotibab
I have a URL like this: http:\/\/www.example.com\/example
in a string object.
我有一个这样的 URL:http:\/\/www.example.com\/example
在一个字符串对象中。
Can somebody tell me, how to remove the backslashes?
有人可以告诉我,如何删除反斜杠?
I am programming for an blackberry system.
我正在为黑莓系统编程。
回答by Kevin Gaudin
See String.replace(CharSequence, CharSequence)
见String.replace(CharSequence, CharSequence)
String myUrl = "http:\/\/www.example.com\/example";
myUrl = myUrl.replace("\","");
回答by Stephen C
@Kevin's answer contains a fatal problem. The String.replace(char, char)
replaces occurrences of one character with another character. It does not remove characters.
@Kevin 的回答包含一个致命的问题。在String.replace(char, char)
用其他字符替换一个字符的出现。它不会删除字符。
Also ''
is not valid Java because there is no such thing as an empty character.
还''
没有有效的Java因为有一个空字符没有这样的事情。
Here are some solutions that should actually work (and compile!):
以下是一些应该实际工作(并编译!)的解决方案:
String myUrl = "http:\/\/www.example.com\/example";
fixedUrl = myUrl.replace("\", "");
This simply removes every backslash without considering the context.
这只是在不考虑上下文的情况下删除了每个反斜杠。
fixedUrl = myUrl.replace("\/", "/");
This replaces escaped forward slashes with plain forward slashes.
这用普通的正斜杠替换了转义的正斜杠。
fixedUrl = myUrl.replaceAll("\\(.)", "\1");
This "de-escapes" anyescape sequences in the String, correctly handling escaped backslashes, and not removing a trailing backslash. This version uses a group to capture the character after the backslash, and then refers to it in the replacement string.
这会“取消转义”字符串中的任何转义序列,正确处理转义的反斜杠,而不是删除尾随的反斜杠。此版本使用一组捕获反斜杠后的字符,然后在替换字符串中引用它。
Note that in the final case we are using the replaceAll
method that does regex match / replace, rather than literal String match / replace. Therefore, we need to escape the backslash in the first argument twice; once because a backslash must be escaped in a String literal, and once because a backslash must be escaped in a regex if you want it to stand for a literal backslash character.
请注意,在最后一种情况下,我们使用的replaceAll
是正则表达式匹配/替换的方法,而不是文字字符串匹配/替换。因此,我们需要对第一个参数中的反斜杠进行两次转义;一次是因为反斜杠必须在字符串文字中转义,一次是因为反斜杠必须在正则表达式中转义,如果您希望它代表文字反斜杠字符。
I am programming for an blackberry system, in case that is of any relevance.
我正在为黑莓系统编程,以防万一。
It is. Blackberry is a J2ME platform, and the J2ME version of String
(see javadoc) onlysupports String.replace(char, char)
, which (as we noted above) cannot remove characters. On a J2ME platform you need to load the String
into a StringBuffer
, and use a loop and StringBuffer.delete(...)
or StringBuffer.replace(...)
.
它是。Blackberry 是一个 J2ME 平台,J2ME 版本String
(见javadoc)只支持String.replace(char, char)
,它(正如我们上面提到的)不能删除字符。在 J2ME 平台上,您需要将 加载String
到 a 中StringBuffer
,并使用循环 andStringBuffer.delete(...)
或StringBuffer.replace(...)
。
(It is stuff like this that makes Android easier to use ...)
(正是这样的东西让 Android 更易于使用......)
回答by Luis Enrique Díaz
Try this:
试试这个:
return myUrl.replaceAll("\\(.)", "\/");
回答by Jyotman Singh
Only this works for me -
只有这对我有用-
String url = "http:\/\/imgd2.aeplcdn.com\/310x174\/cw\/cars\/ford\/ford-figo-aspire.jpg";
for(int i = 0; i < url.length(); i++)
if(url.charAt(i) == '\')
url = url.substring(0, i) + url.substring(i + 1, url.length());