How to replace all characters in a Java string with stars

java string replaceall regex
java replace multiple characters in string
how to replace a character in a string in java without using replace method
remove all occurrences of a character in a string java
remove multiple characters from string java
replace all characters in a string javascript
difference between replace and replaceall in java
replace string between two characters (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());

Note: This replaces newlines \n with *. If you want to preserve \n, 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 \n.

String replaceAll() example - How to replace all , Java 11 and later. str = "*".repeat(str.length());. Note: This replaces newlines \n with * . If you want to preserve \n , see solution below. Java 10 and earlier. str = str  Java String Replaceall() Java String replaceAll() method finds all occurrences of sequence of characters matching a regular expression and replaces them with the replacement string. At the end of call, a new string is returned by the function.

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

Plain Java < 8 Version:

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

Plain Java >= 8 Version:

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:

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

Java String replace(), replaceFirst() and replaceAll() method, You can replace all occurrence of a single character, or a substring of a given String in Java using the replaceAll() method of java.lang.String  Java String replaceAll() replaces all the occurrences of a particular character, string or a regular expression in the string. The method returns a new string as its output. The method requires two parameters.

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

Java String replaceAll() method, Java String replace method signature. String replace(char oldChar, char newChar) : It replaces all the occurrences of a oldChar character with newChar  You need to first find quote and replace, inside it with +. Next you can just use replaceAll (" [^A-Za-z0-9,]", "+") so you will replace all non alphanumeric characters or, with +. Your code for that can use Pattern p = Pattern.compile("\" ([^\"]*)\"");

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

Java.lang.String.replace() in Java, The java string replaceAll() method returns a string replacing all the sequence of characters matching regex and replacement string. Internal implementation. So how do I take a string, and replace every occurrence of * with A-Za-z0-9]* If I do not escape it, it is read as a meta character. If I do escape it, it tells me it is an illegal use of an escape character.

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

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

Java String replace(), replaceFirst() & replaceAll() Method EXAMPLE, String replace():This method returns a new string resulting from replacing all occurrences of old characters in the string with new characters. Syntax: public String  So to remove all the spaces in my string. I did a method that is consists of. message = message.replaceAll("\\s", ""); I was wondering if there was a command to remove and special character, like a comma, or period and just have it be a string.

Java String replace(), Syntax: public Str replace(char oldC, char newC). Parameters: oldCh − old character. newCh − new character. Return Value. This function  In case there are no occurrences of a targeted character, this method will return an unchanged string value which was passed to it. All statements of the above program are terminated by a semi-colon. Learn how to write your own programs in Java with this course. Example 2: Program to Replace Character Sequences import java.lang.String; public

A Java method to replace all instances of a pattern in a String with a , Java String replace() method replaces all existing occurrences of a character in a (String[] args){ //replacing single char String str = "twinkle twinkle little star";  The simple answer is: token = token.replace("&", "&amp;"); Despite the name as compared to replaceAll, replace does do a replaceAll, it just doesn't use a regular expression, which seems to be in order here (both from a performance and a good practice perspective - don't use regular expressions by accident as they have special character requirements which you won't be paying attention to).

Replace Character in a String in Java without using replace() method, I call it like this to create a new string where all integers are replaced with a “ - ” character: String result = findAndReplaceAll("[0-9]", "-"  In this Java tutorial, we will see How to replace characters and substring from String in Java. First example in this program replaces a character, i.e. it replaces "J" with "K", which creates "Kava" from "Java". Second String replace example, replaces words from String, it replaces Scala with Java.

Comments
  • every character including spaces?
  • Note that Sonar will warn with the Java 10 and earlier solution, because of squid:S2639. Rather use a Guava or Commons-lang solution as explained in the other solution.
  • "Don't use . as regular expression, because you probably meant something else"? (source) Quite ridiculous if you ask me.
  • Hey, I am but the messenger 😉. On the other hand, matching everything to get a masked string if the same length is arguably an uncommon usage and a suboptimal solution.
  • Depends what you ootimize for. If you're after conciseness and readability I'd say it's the best solution.
  • I think it should be "You don't need regex at all" rather than "Don't use regex at all". The latter suggests regex is a wrong solution.
  • @taurelas for this problem, it IS the wrong solution. All you need for this problem is the string's length.
  • From Java 11 onwards, there's a repeat method in the String class, so this will be "*".repeat(str.length()) without the need for any non-JDK library.