-2

I have a text like this:

“@joemcelderry91: I've woken up with the flu I think! :( gunna try and
run it off haha!! X” get well soon joey :)

I want to get rid of the double quotes in the sentence. After removal it should be like

@joemcelderry91: I've woken up with the flu I think! :( gunna try and
run it off haha!! X get well soon joey :)

Does anybody have suggestions for removing this so far I have tried this still no luck:

.replaceAll("\"", "");
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Mohan Timilsina
  • 490
  • 6
  • 25
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2608665/how-can-i-trim-beginning-and-ending-double-quotes-from-a-string – jhobbie Jul 06 '14 at 15:13
  • .replaceAll('"','') <- that's a double quote treated like a char – Typo Jul 06 '14 at 15:14

3 Answers3

3

Calling replaceAll() does nothing to the string, because strings are immutable. Instead, replaceAll() returns the modified string, so assign the return value:

str = str.replaceAll("\"", "");

Also... don't use regex!!! Neither your search term nor your replacement need regex, so use the non-regex version of replace:

str = str.replace("\"", "");

Note that despite its name not having "all" in it, replace() still replaces all occurrences.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

It seems like you included a different quotes. So Your regex would be,

"[“”\"]"

Replace it with an empty string.

DEMO

OR

If it's a special double quotes then use the below code to remove it from from the string,

String str = "“@joemcelderry91: I've woken up with the flu I think! :( gunna try and run it off haha!! X” get well soon joey :)";
String s = str.replaceAll("[“”]", "");
System.out.println(s);

Output:

@joemcelderry91: I've woken up with the flu I think! :( gunna try and run it off haha!! X get well soon joey :)

IDEONE

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

What is the error you are getting? This works for me:

String str = "@joemcelderry91: I've woken up with the flu I think! :( gunna try and run it off haha!! X\" get well soon joey :)";
System.out.println(str.replaceAll("\"", ""));

Gave me output:

@joemcelderry91: I've woken up with the flu I think! :( gunna try and run it off haha!! X get well soon joey :)