Java - 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 class "replaceAll" method does the hard work for you, essentially removing any character in the output stream, as it converts the String named aString into a new String named newString. (Remember that you can't modify a String, so you have to assign the output to a new String reference.)

For instance, if our initial 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]","");

the String named newString would now contain these characters:

Fourscoreandsevenyearsago

Again, in this example we'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.

Comments

Permalink

Thanks for sharing! Needed exactly this and Google found your article right away.