Wednesday, June 20, 2007

Replace "." using regexp

String.replaceAll takes a regexp as argument, and a single dot means any character in the regexp world. The solution to replacing a single dot is to escape the dot. A backslash is used for escaping in regular expressions, but we need to use two backslashes. One to escape the dot, and another one to escape the backslash (since we want to place it within a string).

String text = "..text with.. dots..";
String withoutDots = text.replaceAll("\\.", "");

No comments: