I have a string
String a = "dcvdk*vmfdkvm*bmkjfnb*";
I want to replace the * character by space
I tried a.replaceAll("\*", " ");
But it is giving error as invalid escape sequence. Can you please tell me how can I achieve this?
Escape the escape character:
"\\*"
Alternatively, just use replace
, which treats the arguments as literals, not regexes:
a.replace("*", " ")
Or, as Aniket Sahrawat points out, you can use the char
overload in this case:
a.replace('*', ' ')
Remember that the backslash have a special meaning in strings, and you need to escape the backslash itself to get an actual backslash:
a.replaceAll("\\*", " ");