0
String str="India's";
str.replaceAll("'", "");

The above code in my opinion should replace all inverted single commas with nothing in the string.

But the in the output nothing changes.

Can anyone figure out what am I doing wrong?

Thanks

2 Answers2

1

String is immutable, assign it to the same reference

str = str.replaceAll("'", "");
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

String is an immutable class, any change to an object of this class will result in a new object created. Method replaceAll is returning the updated String object. so you should do something like this:

str = str.replaceAll("'","");
iullianr
  • 1,274
  • 10
  • 11