Search code examples
javastringarraylistmasking

preserving whitespace when masking a String


I want to preserve the white space in the String while it is being masked. Below is the code I wrote for masking the string but it doesn't take into account that my some of my strings in my Arraylist have white space.

for (int i=0;i<secretWord.length();i++){
    System.out.print("*");
}

Solution

  • System.out.print(secretWord.replaceAll("\\S","*"));
    

    (In a regular expression, \S means a single character that is not a whitespace character.)