-2

How can I replace a single quotation mark " in a String in Java? Not the word inside a quote.

André Mathisen
  • 904
  • 2
  • 7
  • 15

2 Answers2

6
String s = someotherstring.replace("\"",""); // replace all occurrences of "
EDToaster
  • 3,160
  • 3
  • 16
  • 25
  • (Replace the "" with whatever you want to replace with – EDToaster Jan 30 '15 at 16:21
  • unnecessarily using pattern compiling, as long your replacement is not a `String`. Use String#replace(char, char) instead. – jp-jee Jan 30 '15 at 16:27
  • @jp-jee the replace string isn't guaranteed to be one character only, why is this unnecessary? – EDToaster Jan 30 '15 at 16:29
  • As I said, _as long your replacement is not a String_. – jp-jee Jan 30 '15 at 16:33
  • @jp-jee did you down vote the answer? I find this as an acceptable answer because it provides more uses than String#replace(char,char) and answers the question. You don't expect me to know about their preferences do you? – EDToaster Jan 30 '15 at 16:34
2

Pass it as an argument to String#replace.

Example: To replace any "by an x, use:

 myString.replace('"', 'x');
jp-jee
  • 1,502
  • 3
  • 16
  • 21