Java: How to strip unwanted characters from a string

Here's a quick line of Java code that takes a given input string, strips all the characters from that string other than lowercase and uppercase letters, and returns whatever is left:

String newString = aString.replaceAll("[^a-zA-Z]","");

As you might be able to tell by looking at that line of code, the String replaceAll method does the hard work for you, essentially removing the characters in the output stream that you want to remove as it converts the String named aString into a new String named newString. (Remember that you can't modify a Java string, so you have to assign the output to a new string reference.)

For instance, if our initial string had this content:

aString = "Four (4) score and seven (7) years ago";

and we then ran this replaceAll method on that string, like this:

String newString = aString.replaceAll("[^a-zA-Z]","");

newString will now contain these characters:

Fourscoreandsevenyearsago

Again, in this example I’ve stripped all the characters except the lowercase and uppercase letters.

Strip all characters but letters and numbers

As you might guess, you can strip all characters but letters and numbers by making a minor change to the replaceAll regular expression, like this:

aString.replaceAll("[^a-zA-Z0-9]","");

All I did there was add the numbers [0-9] to our previous range of characters.

Java String character stripping

As you can see from these examples, the Java String class replaceAll method makes it easy to strip/remove characters from one string and assign the resulting output to another string. The only trick in the process is knowing that you can really strip/delete characters from a string using the technique shown here.