• Post Reply Bookmark Topic Watch Topic
  • New Topic

String replaceAll() problem  RSS feed

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have string



when i print this on console, it prints



It works fine. Now what i want to is to print



Which means i have to replace the quotes (") with (\\) and then a quote so that i may get a quote in output. so i used replaceAll method



But to my astonish, it prints out



I wondered and then tried to do it again with



Now i was able to see the desired outout i.e;

But why i have to use 5 back slashes instead of 3 which were according to the logic of "backsequences"?? I have seen the replaceAll method and it is using the pattern matching in it.

If i try to replace the string using simple "replace" method it works fine; i.e i don't have to use 5 backslashes, i use only 3 (according to logic). Can someone explain why i have to use 5 slasheds in replaceAll.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your
System.out.println(s.replaceAll("\"", "\\\""));
does not replace all the quote marks with a String containing a backslash and a quote mark, as you think.
The compiler will see \\\" as \\" because the first pair of \\ is an escape sequence for \, so what it puts in becomes \". So you have changed the String back to where it was before. As you have found out \\\\\" works.
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie
unable to understand ... can you please elaborate
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you put in an escape sequence you lose a \. So when you write \" Java sees "
When you write \\\" Java sees a \\ followed by a \" which comes out to \". it is always awkward counting how many \\ you need.

Instead of using Strings
  • get a StringBuilder,
  • iterate through it with an index number,
  • whenever you get to charAt(index) == (char)0x22, insert (char)0x5c and ncrease index by one.
  • Continue until you finish the StringBuilder, and return the result as a new String.

  • 0x22 is " and 0x5c (I think) is \.

    CR

    Anybody better able to explain how escape sequences work and how to count the \\\\\\ ?
    [ April 13, 2007: Message edited by: Campbell Ritchie ]
     
    Stefan Wagner
    Ranch Hand
    Posts: 1923
    Linux Postgres Database Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One technique to find out, what's going on, is the allmost secret System.out.println-Method:



    Another problem-solver is a JTextField, where you get the replacement from.

    Then you have two separate Problems:
    - is the core-statement correct?
    - transform it, to make it sourcecode-ready.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Remember that the second parameter of the replaceAll() method is not just a string. It is also a regex replacement string. The easiest way to figure out the correct number of backslashes is to deal with the regex first, and then the java string second.

    What you want is........ \"

    First, in regex (replacement string), the backslash has special meaning, so you will need to escape that.

    Now you have........... \\"

    Second, in Java String, the backslash and the quote has special meaning, so you will need to escape all three.

    Giving you a final result of ......... \\\\\"

    Hope this helps,
    Henry
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!