0

I have following StringBuilder and I want to replace ' with " but I am not able to achieve the same.

Code :

    StringBuilder sb = new StringBuilder("'DateTime',");
    String word = sb.toString().replace("'",""");

At line number two, I am getting compilation error. I tried same method for other character and its wroked poper but for " character I am not able to achieve with same method. Might be its little simple but still I am not able to figure out.

I am trying to get following output.

Output : "DateTime",

vijayk
  • 2,633
  • 14
  • 38
  • 59
  • 2
    If we want `"` as character in a `String`, we need to escape it with a backslash (`"\""`). [Ideone demo](https://ideone.com/MR8Nc9). – Turing85 Jan 04 '22 at 20:23
  • 1
    BTW do not use `replaceAll` - there is no reason to use regular expression for such a *simple* task... use `replace` instead (also no reason to use a `StringBuilder` in given code) – user16320675 Jan 04 '22 at 21:30

0 Answers0