java⽤户名部分星号替换,如何⽤星号替换Java字符串中的所有
字符
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
str = "*".repeat(str.length());
java replace方法Note: This replaces newlines \n with *. If you want to preserve \n, see solution below.
Java 10 and earlier
str = placeAll(".", "*");
This preserves newlines.
To replace newlines with * as well in Java 10 and earlier, you can use:
str = placeAll("(?s).", "*");
The (?s) doesn't match anything but activates DOTALL mode which makes . also match \n.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论