I want to replace all the characters in a Java String with * character. So it shouldn’t matter what character it is, it should be replaced with a *.
I know there are heaps of examples there on internet but have not one that replaces every character and I have tried myself but no success.
Java 11 and later
Note: This replaces newlines
\nwith*. If you want to preserve\n, see solution below.Java 10 and earlier
This preserves newlines.
To replace newlines with
*as well in Java 10 and earlier, you can use:The
(?s)doesn’t match anything but activatesDOTALLmode which makes.also match\n.