Replace dash character in Java String

wawanopoulos picture wawanopoulos · Jul 20, 2013 · Viewed 18.7k times · Source

I tried to replace "-" character in a Java String but is doesn't work :

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

Could you help me ?

Answer

Suresh Atta picture Suresh Atta · Jul 20, 2013

String is Immutable in Java. You have to reassign it to get the result back:

String str ="your string with dashesh";
str= str.replace("\u2014", "");

See the API for details.