2

Hi we have a string like "ami\\303\\261o". we want to replace \\ with \.

We have tried the following:

  1. replace("\\", "\")
  2. replaceAll("\\", "\")

But we didn't get proper output.

6
  • 6
    Is "ami\\303\\261o" the "real" string or the string literal in Java ? Commented Mar 13, 2013 at 11:42
  • I assume in Java, \ is the escape character, so \\ will produce a literal backslash. So the string will actually only contain \, not \\. How exactly do you want the result to look like? Commented Mar 13, 2013 at 11:42
  • 2
    @FelixKling your edit seems to answer the question rather than fix the question. What's the story here? Commented Mar 13, 2013 at 11:44
  • 3
    @mah: All I did was adding backticks because the \ where not rendered properly. I did not change the content of string or code (see the markdown side-by-side comparison). Commented Mar 13, 2013 at 11:46
  • 2
    The edit history standard view is very confusing here... Commented Mar 13, 2013 at 11:50

5 Answers 5

3

For use in a Java regex, you need to escape the backslashes twice:

resultString = subjectString.replaceAll("\\\\\\\\", "\\\\");
  1. In a regex, \\ means "a literal backslash".
  2. In a Java string, "\\" encodes a single backslash.
  3. So, a Java string that describes a regex that matches a single backslash is "\\\\"
  4. And if you want to match two backslashes, it's "\\\\\\\\", accordingly.
Sign up to request clarification or add additional context in comments.

3 Comments

This. Is. Ugly. Is there a better way?
There is little reason to use replaceAll in this case - replace is much clearer.
@assylias: Certainly, but the OP specifically asked about a regex.
3

You must keep backslash escaping in mind. Use

public class so {
    public static void main(String[] args) {
        String s = "ami\\\\303\\\\261o";
        System.out.println(s);
        s = s.replace("\\\\", "\\");
        System.out.println(s);
    }
};

Each backslash escapes the following backslash and resolves to the two literal strings \\ and \

Also keep in mind, String.replace returns the modified string and keeps the original string intact.

Comments

3

No need of regex here. Escape the slashes and use replace()

someString.replace('\\\\', '\\');

1 Comment

You should replace the ' with ".
0

Thats because the \\ inside your input String get internally replaced by \ because of the Java Escape Character.
That means that if you output your String without performing any regex on it, it would look like this: "ami\303\261o".

Generally you should remember to escape every escape-character with itself:

\ -> escaped = \\  
\\ -> escaped = \\\\  
\\\ -> escaped = \\\\\\  
...and so on  

1 Comment

String expectedString="ami\303\261o"; its ouput is like: amiño String str="ami\\303\\261o"; its output is like: ami\303\261o we required String 'amiño'.
0

Try below code

 String val = "ami\\303\\261o"; 
    val =val.replaceAll("\\\\", "\\\\");        
    System.out.println(val);

Outpout would be

ami\303\261o

A Fiddle is created here check it out

Java Running Example

4 Comments

The original string was the same, the replace does nothing here.
I tried and run the above program and added the output in the post
Try to run this code then: String val = "ami\\303\\261o"; System.out.println(val); The output is still ami\303\261o.
that is the answer OP is looking for. Ok I am adding more to post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.