As pointed out by others, you could use regular expressions to clean up your String and replace unwanted part by an empty string ""
. To do so, have a look at the replaceAll(String regex, String replacement)
method of the String
class and at the Pattern
class for the syntax of regular expressions in Java.
Below, some code demonstrating one way to clean the provided sample String (maybe not the most elegant though):
String input = "Hello world my # is 123 mail me @ [email protected]";
String EMAIL_PATTERN = "([^.@\\s]+)(\\.[^.@\\s]+)*@([^.@\\s]+\\.)+([^.@\\s]+)";
String output = input.replaceAll(EMAIL_PATTERN, "") // Replace emails
// by an empty string
.replaceAll("\\p{Punct}", "") // Replace all punctuation. One of
// !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
.replaceAll("\\d", "") // Replace any digit by an empty string
.replaceAll("\\p{Blank}{2,}+", " "); // Replace any Blank (a space or
// a tab) repeated more than once
// by a single space.
System.out.println(output);
Running this code produces the following output:
Hello world my is mail me
If you need to remove more garbage (or less, like punctuation), well, you've got the principle. Adapt it to suit your needs.