How do you replace double quotes with a blank space in Java?

前端 未结 4 1055
谎友^
谎友^ 2020-12-16 10:55

For example:

\"I don\'t like these \"double\" quotes\"

and I want the output to be

I don\'t like these double quotes


        
相关标签:
4条回答
  • 2020-12-16 11:06

    You can do it like this:

    string tmp = "Hello 'World'";
    tmp.replace("'", "");
    

    But that will just replace single quotes. To replace double quotes, you must first escape them, like so:

    string tmp = "Hello, \"World\"";
    tmp.replace("\"", "");
    

    You can replace it with a space, or just leave it empty (I believe you wanted it to be left blank, but your question title implies otherwise.

    0 讨论(0)
  • 2020-12-16 11:11

    You don't need regex for this. Just a character-by-character replace is sufficient. You can use String#replace() for this.

    String replaced = original.replace("\"", " ");
    

    Note that you can also use an empty string "" instead to replace with. Else the spaces would double up.

    String replaced = original.replace("\"", "");
    
    0 讨论(0)
  • 2020-12-16 11:20

    Use String#replace().

    To replace them with spaces (as per your question title):

    System.out.println("I don't like these \"double\" quotes".replace("\"", " "));
    

    The above can also be done with characters:

    System.out.println("I don't like these \"double\" quotes".replace('"', ' '));
    

    To remove them (as per your example):

    System.out.println("I don't like these \"double\" quotes".replace("\"", ""));
    
    0 讨论(0)
  • 2020-12-16 11:22

    Strings are immutable, so you need to say

    sInputString = sInputString("\"","");
    

    not just the right side of the =

    0 讨论(0)
提交回复
热议问题