String replace a Backslash
Asked Answered
B

8

44

How can I do a string replace of a back slash.

Input Source String:

sSource = "http://www.example.com\/value";

In the above String I want to replace "\/" with a "/";

Expected ouput after replace:

sSource = "http://www.example.com/value";

I get the Source String from a third party, therefore I have control over the format of the String.

This is what I have tried

Trial 1:

sSource.replaceAll("\\", "/");

Exception Unexpected internal error near index 1 \

Trial 2:

 sSource.replaceAll("\\/", "/");

No Exception, but does not do the required replace. Does not do anything.

Trial 3:

 sVideoURL.replace("\\", "/"); 

No Exception, but does not do the required replace. Does not do anything.

Bum answered 8/4, 2011 at 14:18 Comment(3)
btw, you source cannot be \/ if you declare it as a string literal. But I will assume it is coming from somewhere else.Prepositive
In your example, you replace \ with nothing, not with /; there is already a forward slash.Contributory
possible duplicate of Backslash problem with String.replaceAllOwnership
P
56
sSource = sSource.replace("\\/", "/");
  • String is immutable - each method you invoke on it does not change its state. It returns a new instance holding the new state instead. So you have to assign the new value to a variable (it can be the same variable)
  • replaceAll(..) uses regex. You don't need that.
Prepositive answered 8/4, 2011 at 14:20 Comment(0)
H
39

Try replaceAll("\\\\", "") or replaceAll("\\\\/", "/").

The problem here is that a backslash is (1) an escape chararacter in Java string literals, and (2) an escape character in regular expressions – each of this uses need doubling the character, in effect needing 4 \ in row.

Of course, as Bozho said, you need to do something with the result (assign it to some variable) and not throw it away. And in this case the non-regex variant is better.

Hoop answered 8/4, 2011 at 14:22 Comment(0)
S
14

Try

   sSource = sSource.replaceAll("\\\\", "");

Edit : Ok even in stackoverflow there is backslash escape... You need to have four backslashes in your replaceAll first String argument...

The reason of this is because backslash is considered as an escape character for special characters (like \n for instance).
Moreover replaceAll first arg is a regular expression that also use backslash as escape sequence.
So for the regular expression you need to pass 2 backslash. To pass those two backslashes by a java String to the replaceAll, you also need to escape both backslashes.
That drives you to have four backslashes for your expression! That's the beauty of regex in java ;)

Synge answered 8/4, 2011 at 14:20 Comment(3)
Won't work. use replace() instead of replaceAll() or use four backslashesStannum
For stackoverflow syntax, use `` or four spaces in front to disable parsing (and format it as code).Antidromic
Yep I didn't see that my backslashes were not escaped :) I edited my answerSynge
C
7
s.replaceAll ("\\\\", "");

You need to mask a backslash in your source, and for regex, you need to mask it again, so for every backslash you need two, which ends in 4.

But

s = "http://www.example.com\\/value";

needs two backslashes in source as well.

Contributory answered 8/4, 2011 at 14:31 Comment(0)
P
5

This will replace backslashes with forward slashes in the string:

source = source.replace('\\','/');
Pence answered 6/8, 2014 at 20:42 Comment(1)
Plain and simple :D and no regexp ;)Anisomerous
M
2

you have to do

sSource.replaceAll("\\\\/", "/");

because the backshlash should be escaped twice one for string in source one in regular expression

Momism answered 8/4, 2011 at 14:24 Comment(0)
M
1

To Replace backslash at particular location:

if ((stringValue.contains("\\"))&&(stringValue.indexOf("\\", location-1)==(location-1))) {
    stringValue=stringValue.substring(0,location-1);
}
Martini answered 1/3, 2017 at 10:11 Comment(0)
M
0
 sSource = StringUtils.replace(sSource, "\\/", "/")
Martingale answered 15/10, 2014 at 11:8 Comment(1)
Could you explain what was wrong with OP's code and why this one is better to use?Vanish

© 2022 - 2024 — McMap. All rights reserved.