How can I replace a single quotation mark "
in a String
in Java? Not the word inside a quote.
Asked
Active
Viewed 1,598 times
-2

André Mathisen
- 904
- 2
- 7
- 15
-
What did you try? Are you asking how to put a `"` in a string? – Jan 30 '15 at 16:19
-
1string = string.replaceAll(""", ""); – André Mathisen Jan 30 '15 at 16:20
-
Try \" adding \ would treat (") as a character and not the default interpretation of compiler. – Aadil Keshwani Jan 30 '15 at 16:27
-
1@AadilKeshwani backslash not foward slash ;) – EDToaster Jan 30 '15 at 16:30
2 Answers
6
String s = someotherstring.replace("\"",""); // replace all occurrences of "

EDToaster
- 3,160
- 3
- 16
- 25
-
-
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
-
-
@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
-
1
-
1Why would I? No opening/closing quotes would be disturbed. It's a `char`. Perfectly working. – jp-jee Jan 30 '15 at 16:22
-