1

What would I need instead of "" to replace all alphabetical letters with * ?

public static void main(String[] args) {
    String s = "Tere, TULNUKAS, 1234!";
    String t = asenda(s); // "****, ********, 12345!" <---- example 
}

public static String asenda(String s) {
    return s.replaceAll("", "*");
}    

Thanks!

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
user3086917
  • 97
  • 2
  • 12

3 Answers3

4

You have to use regular expression:

return s.replaceAll("[a-zA-z]", "*")
Jakub Kubrynski
  • 13,724
  • 6
  • 60
  • 85
2

For every letter you can use the [a-zA-Z] regex

For example:

public static String asenda(String s) {
    return s.replaceAll("[a-zA-Z]", "*");
} 
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

Proper solution with unicode characters support is

public static String asenda(String s) {
    return s.replaceAll("\\p{L}", "*");
}

You can match a single character belonging to the "letter" category with \p{L}.

Source: Unicode Regular Expressions/Unicode Categories

More information:

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155