String str = "Welcome to plus2net.com";
System.out.println(str);
System.out.println(str.replaceAll("[m-p]","*"));
Output is here
Welcome to plus2net.com
Welc**e t* *lus2*et.c**
Chars m, n, o, p are replaced with a * in above code.
String str = "Welcome to plus2net.com";
System.out.println(str);
System.out.println(str.replaceAll("[0-9]","*"));
Output
Welcome to plus*net.com
We can also replace all digits by this code
System.out.println(str.replaceAll("\\d","*"));
Let us match the word boundaries and replace them with *
System.out.println(str.replaceAll("\\b","*"));
Output is here ( note our variable str is same as in above codes, we are not repeating here. )
*Welcome* *to* *plus2net*.*com*
Replace all Non digits present in the string
System.out.println(str.replaceAll("\\D","*"));
Output
***************2*******
Replace all upper case chars only.
String str1 = "Welcome to Plus2Net.com";
System.out.println(str1.replaceAll("\\p{Upper}","*"));
Output
*elcome to *lus2*et.com
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.