string.replaceAll()
String.replaceAll() regular expression string replacement .
Here are some examples with output.
Replacing all words starting from m to p
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.
Let us replace all digits.
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
« All String functions
This article is written by plus2net.com team.
plus2net.com