1

I have a string value that includes backslash character (\). My goal is to replace it with the character(_).

I did the following as per solution provided in this question How to remove the backslash in string using regex in Java?

String x = "2\5\2017"; x = x.replaceAll("\\\\", "_");

but I am always getting (27) as a value of x. Note: there is unknown character between 2 and 7. In the console, I can see it as a small square.

Community
  • 1
  • 1
Salman
  • 1,236
  • 5
  • 30
  • 59
  • Did you try both solutions ? ( `replaceAll` and `replace` ). PS : You need to escape the `\ ` in `x` too -> `String x = "2\\5\\2017";` – AxelH May 02 '17 at 06:06
  • 1
    In string literals, the backslash is an escape character, meaning that `"2\5\2017"` is interpreted as 4 characters, the middle two of which have ASCII values `5` and `201` (as octal digits). You need to escape your backslashes in string literals: `"2\\5\\2017"` – 4castle May 02 '17 at 06:09
  • @AxelH Yes I did both replace and replaceAll and I got the same result. The String value is coming from DB like this it is not hard coded value to add a double slash. – Salman May 02 '17 at 06:10
  • _I have a string value that includes backslash character (/). My goal is to replace it with the character(_)._ your example uses a different slash... "/" vs. "\" –  May 02 '17 at 06:11
  • 1
    Your problem is not in `replace` - but in the original string. (This is also mentioned by others). To confirm that - just try printing the original string - without applying any replace... see what happens. – PKey May 02 '17 at 06:13
  • 1
    @Salman Can you confirm that the value you have shown us in your example is the result of a `System.out.print(x);` If that's the case, then you String is correct, but since you have a an output `2##7` then I guess not. Then you should check your method to get that value because the problem come from the DB or the JDBC. – AxelH May 02 '17 at 06:40
  • @AxelH and Plirkee, you are correct the original value is missed up. I should manage to get data correctly from DB – Salman May 02 '17 at 06:52

4 Answers4

5

This is caused by you are not escaping your \ in your x("2\5\2017"), if your doesn't escape \ in x, it will think \5 and \201 (octal will have 3 digits) will be octal number. so:

"2\5\2017"

will display as:

2??7

so you should unescape like @Harmlezz way.

chengpohi
  • 14,064
  • 1
  • 24
  • 42
  • String value is coming from DB as "2\5\2017" without double slash how to apply @Harmlezz solution to this case – Salman May 02 '17 at 06:17
  • 1
    @Salman do you mean that if you print out string value the moment it comes from Db then you get `2\5\2017` in your console and not something like `2??7` (as @chengpohi mentions)? In that case your example is flawed.. cause "2\\5\\2017" prints out as `2\5\2017`.... – PKey May 02 '17 at 06:26
4

I think you want to redefine your input string to:

String x = "2\\5\\2017"

then the result of x would be:

2_5_2017
Harmlezz
  • 7,972
  • 27
  • 35
0

Just use " \\ " to add " \ " in your string. and then replace it with " _ "

String x = "2\\5\\2017"; x = x.replaceAll("\\\\", "_");

" \ " is a special character ,to see "\" in our String we need to add one extra "\" like "\\" in a string.

learn more on Special Characters

Vivek
  • 11
  • 3
0

As mentioned by Plirkee, AxelH and chengpohi, the original value is not correct.

We manage to get a different form from DB (2/5/2017) instead of (2\5\2017) and then the replaceAll works.

Salman
  • 1,236
  • 5
  • 30
  • 59