I tried:
mystring.replace("'","");
and also
mystring.replaceAll("[']","");
But none of them work, please show me how to remove that '
from my string.
I tried:
mystring.replace("'","");
and also
mystring.replaceAll("[']","");
But none of them work, please show me how to remove that '
from my string.
Are you assinging the result of this method? You should be calling it like this :
mystring = mystring.replace("'","");
Strings in Java are immutable - when you call replace, it doesn't change the contents of the existing string - it returns a new string with the modifications. So you want:
This is why you have to assign the return value to a string.
As a note, this also applies to all methods in String
. Methods like toUpperCase()
return the new string. It does not change the existing.
First, for better help faster please post an MCVE. Next, Java String
is immutable, so you must update the mystring
reference. Something like,
String mystring = "Papa John's";
mystring = mystring.replace("'","");
System.out.println(mystring);
Output is (as requested) without '
Papa Johns