3

I have a URL like this: http:\/\/www.example.com\/example in a string object.

Can somebody tell me, how to remove the backslashes?

I am programming for an blackberry system.

0m3r
  • 12,286
  • 15
  • 35
  • 71
Kotibab
  • 85
  • 1
  • 1
  • 7

4 Answers4

9

See String.replace(CharSequence, CharSequence)

String myUrl = "http:\\/\\/www.example.com\\/example";
myUrl = myUrl.replace("\\","");
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Kevin Gaudin
  • 9,927
  • 3
  • 32
  • 34
  • I just forgot to escape the backslahes in order to allow to put them in a String constant (edited). I assume the String you are receiving is not declared this way. – Kevin Gaudin Feb 24 '11 at 11:57
  • @Kevin: THis is confusing I don't know why but "Kotibab" seems to have "\/", which - I guess should be replaced by "/". So @Kotibab: you might want to try myUrl.replace("\",""); (is it the single or double quotes, I'm not sure, but should be double, as I put it.) – AudioDroid Feb 24 '11 at 12:00
  • 3
    @Kevin, I think you meant `myUrl.replace("\\/","/");` or `myUrl.replace("\\","");` as there is no such thing as the empty character. ;) – Peter Lawrey Feb 24 '11 at 12:09
  • @Kotibab: If you "tried another method", would you be so kind to share it, and tell us and others with the same problem (in the future) how to solve it? That's the idea of this platform. "-1", if I could downgrade a comment! You can post an answer to your own question, and mark it as the right answer. – AudioDroid Feb 25 '11 at 12:47
6

@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.

Also '' is not valid Java because there is no such thing as an empty character.

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" any escape 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.


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) only supports 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(...).

(It is stuff like this that makes Android easier to use ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Try this:

return myUrl.replaceAll("\\\\(.)", "\\/");
0

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());
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55