0

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?

sky
  • 260
  • 3
  • 12

2 Answers2

3

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('*', ' ')
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

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("\\*", " ");
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621