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.
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.
See String.replace(CharSequence, CharSequence)
Try this:
@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!):
This simply removes every backslash without considering the context.
This replaces escaped forward slashes with plain forward slashes.
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.It is. Blackberry is a J2ME platform, and the J2ME version of
String
(see javadoc) only supportsString.replace(char, char)
, which (as we noted above) cannot remove characters. On a J2ME platform you need to load theString
into aStringBuffer
, and use a loop andStringBuffer.delete(...)
orStringBuffer.replace(...)
.(It is stuff like this that makes Android easier to use ...)
Only this works for me -