I\'m wondering if I can use string.replace()
to replace all alphabets in a string?
String sentence = \"hello world! 722\"
String str = sentence
Use String#replaceAll that takes a Regex:
str = str.replaceAll("[a-zA-Z]", "@");
Note that String#replace takes a String as argument and not a Regex. If you still want to use it, you should loop on the String char-by-char and check whether this char is in the range [a-z] or [A-Z] and replace it with @
. But if it's not a homework and you can use replaceAll
, use it :)