Java 11 and later
str = "*".repeat(str.length());
Note: This replaces newlines
with *
. If you want to preserve
, see solution below.
Java 10 and earlier
str = str.replaceAll(".", "*");
This preserves newlines.
To replace newlines with *
as well in Java 10 and earlier, you can use:
str = str.replaceAll("(?s).", "*");
The (?s)
doesn't match anything but activates DOTALL
mode which makes .
also match
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…