replace double inverted commas into slash double inverted commas in string

Discussion in 'Java' started by AviraM, Sep 28, 2006.

  1. AviraM

    AviraM Guest

    hi all

    I have a var of String type
    the string include double inverted commas (") and I need to replace it
    into slash double inverted commas (\")

    example:
    original: "abc"de"
    result: "abc\"de"

    that problem came because I got the string from the databse and I need
    the string to make a query from the databae comparing as is but the
    all parse is illegal without change my string

    thanks all
     
    AviraM, Sep 28, 2006
    #1
    1. Advertisements

  2. If I were better with regex I could probably do this in one step :).

    public class test8 {
    public static void main(String[] args) {
    String str = "\"abc\"def \"13s\"";
    System.out.println(str);
    str = str.replaceAll("\"","\\\\\"");
    System.out.println(str);
    str = str.replaceAll("^\\\\\"(.*)\\\\\"$","\"$1\"");
    System.out.println(str);
    }
    }
     
    Knute Johnson, Sep 28, 2006
    #2
    1. Advertisements

  3. Manish Pandit, Sep 28, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.