String replace with "\"


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: String replace with "\"

  1. #1
    Join Date
    Sep 2004
    Posts
    2

    Question String replace with "\"

    I just want to replace in a string all occurrences of the ' caracter by the double caracters "\\\"", so for example:

    Hello 'big' world
    should become
    Hello \"big\" world

    But the replace method of String looses my backslach caracter in the replacement string.
    Look:
    public static void main(String[] args) {

    String string="Hello 'big' world";
    System.out.println("Before:" + string + " length=" + string.length());
    String rpstring = "\\\"";
    string=string.replaceAll("'", rpstring);
    System.out.println("After:" + string + " length=" + string.length());
    System.out.println("repalce string:" + rpstring + " length=" + rpstring.length());
    }

    That gives:
    Before:Hello 'big' world length=17
    After:Hello "big" world length=17
    repalce string:\" length=2


    How can I do ?

    Thanks,
    Bernard

  2. #2
    Join Date
    Sep 2004
    Posts
    1
    Hi Bernard,
    I don't know whats ur need. If its just to convert all the single quote character to double quote character, than ur code 100% correct. This is because ur getting
    hello "big" world after replacing which means ur string variable value is "hello \"Big\" world".

    If ur requirement is something else than make the following change.....
    String rpstring = "\\\\\\\"";

    And this will work.

    Kalai Selvan T.

  3. #3
    Join Date
    Sep 2004
    Posts
    2

    Thumbs up

    Hi Kalai,

    String rpstring = "\\\\\\\"";

    thats's what I needed,
    thanks a lot.
    BP.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center