How to remove “ ” from java string

后端 未结 9 1545
遥遥无期
遥遥无期 2020-12-17 07:31

I have a java string with \" \" from a text file the program accesses with a Buffered Reader object. I have tried string.replaceAll(\" 

相关标签:
9条回答
  • 2020-12-17 08:22

    String.replace(char, char) takes char inputs (or CharSequence inputs)

    String.replaceAll(String, String) takes String inputs and matches by regular expression.

    For example:

    String origStr = "bat";
    String newStr = str.replace('a', 'i');
    // Now:
    // origStr = "bat"
    // newStr = "bit"

    The key point is that the return value contains the new edited String. The original String variable that invokes replace()/replaceAll() doesn't have its contents changed.

    For example:

    String origStr = "how are you?";
    String newStr = origStr.replaceAll(" "," ");
    String anotherStr = origStr.replaceAll(" ","");
    // origStr = "how are you?"
    // newStr = "how are you?"
    // anotherStr = howareyou?"
    
    0 讨论(0)
  • 2020-12-17 08:26

    The same way you mentioned:

    String cleaned = s.replace(" "," ");
    

    It works for me.

    0 讨论(0)
  • 2020-12-17 08:27

    There's a ready solution to unescape HTML from Apache commons:

    StringEscapeUtils.unescapeHtml("")
    

    You can also escape HTML if you want:

    StringEscapeUtils.escapeHtml("")
    
    0 讨论(0)
提交回复
热议问题