2

I tried:

mystring.replace("'","");

and also

mystring.replaceAll("[']","");

But none of them work, please show me how to remove that ' from my string.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Bụng Bự
  • 553
  • 1
  • 7
  • 15

2 Answers2

2

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.

Ryan
  • 1,972
  • 2
  • 23
  • 36
2

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
Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249