如何用星号替换Java字符串中的所有字符

时间:2022-08-22 13:16:32

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 *.

我想用*字符替换Java字符串中的所有字符。所以不管它是什么字符,它都应该用*来代替。

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.

我知道网上有很多这样的例子,但是没有一个可以取代所有的角色,我也尝试过,但是没有成功。

7 个解决方案

#1


44  

str = str.replaceAll(".", "*");

Note: This will not replace line breaks (\n) with *. To do this, you'll need to use

注意:这将不会替换(\n)与*的换行。为此,您需要使用。

str = str.replaceAll("(?s).", "*");

The (?s) doesn't match anything but activates DOTALL mode which makes . also match \n.

除了激活DOTALL模式外,这个(? ? ?)并不匹配任何东西。也匹配\ n。

#2


11  

Don't use regex at all, count the String length, and return the according number of stars.

不要使用regex,计算字符串长度,并返回相应的星号。

Plain Java < 8 Version:

普通Java < 8版本:

int len = str.length();
StringBuilder sb = new StringBuilder(len);
for(int i = =; i < len; i++){
    sb.append('*');
}
return sb.toString();

Plain Java >= 8 Version:

普通Java >= 8版本:

int len = str.length();
return IntStream.range(0, n).mapToObj(i -> "*").collect(Collectors.joining());

Using Guava:

使用番石榴:

return Strings.repeat("*", str.length());
// OR
return CharMatcher.ANY.replaceFrom(str, '*');

Using Commons / Lang:

使用共享/ Lang:

return StringUtils.repeat("*", str.length());

#3


4  

System.out.println("foobar".replaceAll(".", "*"));

#4


3  

public String allStar(String s) {
    StringBuilder sb = new StringBuilder(s.length());
    for (int i = 0; i < s.length(); i++) {
        sb.append('*');
    }
    return sb.toString();
}

#5


1  

How abt creating a new string with the number of * = number of last string char?

abt如何用* =最后一个字符串char的数量创建一个新的字符串?

StringBuffer bf = new StringBuffer();
for (int i = 0; i < source.length(); i++ ) {
    bf.append('*');
}

#6


1  

There may be other faster/better ways to do it, but you could just use a string buffer and a for-loop:

可能还有其他更快/更好的方法,但是您可以使用字符串缓冲区和for-loop:

public String stringToAsterisk(String input) {
    if (input == null) return "";

    StringBuffer sb = new StringBuffer();
    for (int x = 0; x < input.length(); x++) {
        sb.append("*");
    }
    return sb.toString();
}

If your application is single threaded, you can use StringBuilder instead, but it's not thread safe.

如果应用程序是单线程的,可以使用StringBuilder,但它不是线程安全的。

I am not sure if this might be any faster:

我不确定这是否会更快:

public String stringToAsterisk(String input) {
    if (input == null) return "";

    int length = input.length();
    char[] chars = new char[length];
    while (length > 0) chars[--length] = "*";
    return new String(chars);
}

#7


0  

Without any external library and without your own loop, you can do:

没有任何外部库,没有您自己的循环,您可以做:

String input = "Hello";
char[] ca = new char[input.length()];
Arrays.fill(ca, '*');
String output = new String(ca);

BTW, both Arrays.fill() and String(char []) are really fast.

填充()和字符串(char[])都非常快。

#1


44  

str = str.replaceAll(".", "*");

Note: This will not replace line breaks (\n) with *. To do this, you'll need to use

注意:这将不会替换(\n)与*的换行。为此,您需要使用。

str = str.replaceAll("(?s).", "*");

The (?s) doesn't match anything but activates DOTALL mode which makes . also match \n.

除了激活DOTALL模式外,这个(? ? ?)并不匹配任何东西。也匹配\ n。

#2


11  

Don't use regex at all, count the String length, and return the according number of stars.

不要使用regex,计算字符串长度,并返回相应的星号。

Plain Java < 8 Version:

普通Java < 8版本:

int len = str.length();
StringBuilder sb = new StringBuilder(len);
for(int i = =; i < len; i++){
    sb.append('*');
}
return sb.toString();

Plain Java >= 8 Version:

普通Java >= 8版本:

int len = str.length();
return IntStream.range(0, n).mapToObj(i -> "*").collect(Collectors.joining());

Using Guava:

使用番石榴:

return Strings.repeat("*", str.length());
// OR
return CharMatcher.ANY.replaceFrom(str, '*');

Using Commons / Lang:

使用共享/ Lang:

return StringUtils.repeat("*", str.length());

#3


4  

System.out.println("foobar".replaceAll(".", "*"));

#4


3  

public String allStar(String s) {
    StringBuilder sb = new StringBuilder(s.length());
    for (int i = 0; i < s.length(); i++) {
        sb.append('*');
    }
    return sb.toString();
}

#5


1  

How abt creating a new string with the number of * = number of last string char?

abt如何用* =最后一个字符串char的数量创建一个新的字符串?

StringBuffer bf = new StringBuffer();
for (int i = 0; i < source.length(); i++ ) {
    bf.append('*');
}

#6


1  

There may be other faster/better ways to do it, but you could just use a string buffer and a for-loop:

可能还有其他更快/更好的方法,但是您可以使用字符串缓冲区和for-loop:

public String stringToAsterisk(String input) {
    if (input == null) return "";

    StringBuffer sb = new StringBuffer();
    for (int x = 0; x < input.length(); x++) {
        sb.append("*");
    }
    return sb.toString();
}

If your application is single threaded, you can use StringBuilder instead, but it's not thread safe.

如果应用程序是单线程的,可以使用StringBuilder,但它不是线程安全的。

I am not sure if this might be any faster:

我不确定这是否会更快:

public String stringToAsterisk(String input) {
    if (input == null) return "";

    int length = input.length();
    char[] chars = new char[length];
    while (length > 0) chars[--length] = "*";
    return new String(chars);
}

#7


0  

Without any external library and without your own loop, you can do:

没有任何外部库,没有您自己的循环,您可以做:

String input = "Hello";
char[] ca = new char[input.length()];
Arrays.fill(ca, '*');
String output = new String(ca);

BTW, both Arrays.fill() and String(char []) are really fast.

填充()和字符串(char[])都非常快。