Showing posts with label replaceAll. Show all posts
Showing posts with label replaceAll. Show all posts

Wednesday, November 13, 2013

how to replace all characters in a java string

String line = "Pritom K Mondal";
line = line.replaceAll("(?s).", "X");

Will output:
XXXXXXXXXXXXXXX
 
The (?s) doesn't match anything but sets the DOTALL flag.

Java - Replace all non digits with an empty character in a string

String cardNumber = "4444 3333 2222 111");
cardNumber = cardNumber.trim();
cardNumber = cardNumber.replaceAll("\\D+", "");

Output:
4444333322221111