Removing all digits, non-digits, whitespaces (from Strings) and other usages of replaceAll

Some tasks will require you to replace all occurrences of certain Strings from other Strings with something else, based on a pattern. Or remove all those occurrences. For example, maybe you want to only keep the numeric characters of a String. Or remove all numeric characters. Or maybe remove all white spaces. For this, the replaceAll method comes in handy.

The replaceAll method is part of Java’s String class, and is documented here: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-.

A method call would look something like this: given that the variable ‘stringVariable’ is the String in which you need to do the replacement:

stringVariable.replaceAll(regex, replacement)

The first parameter, the regex, needs to be, as the name calls it, a regex, as a String. The replacement simply needs to be the String that you want to occur in your initial String where the characters identified by the regex used to be. A good list of regex’s that are usable with this method can be found here: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html.

Some fun examples

Removing all digits (numeric characters) from String

For the given String, the replaceAll command using the \d pattern will be used to remove all numeric characters, as in the below example. No character will be inserted to replace the digits.

String complexString = "3ifhuq023hjk@jka$ksoap";
complexString.replaceAll("\\d", "")

The resulting String is :

ifhuqhjk@jka$ksoap

Removing all non-digits characters

For the given String the replaceAll command using the \D pattern will remove all non digit characters, as in the below example. No character will be used to replace the removed characters.

String complexString = "3ifhuq023hjk@jka$ksoap";
complexString.replaceAll("\\D", "")

The resulting String is :

3023

Removing all new line characters

By using the replaceAll method with the \n pattern, all new line character are removed from the given String. Any other whitespace character is kept inside the String.

String withWhiteSpacesString = "This is a string\n " +
        "that streches on several lines.\n" +
        "to demo how to remove whitespaces and       newlines\n " +
        "from a\n" +
        "string\n";

What the String looks like before the removeAll is applied:

This is a string
that streches on several lines.
to demo how to remove whitespaces and newlines
from a
string

Removing the new lines is done like this:

withWhiteSpacesString.replaceAll("\n", "")

The resulting String is:

This is a string that streches on several lines.to demo how to remove whitespaces and newlines from astring

Removing all whitespaces

Removing all the whitespace characters from a String (including new line and space), the \s pattern is used.

String withWhiteSpacesString = "This is a string\n " +
        "that streches on several lines.\n" +
        "to demo how to remove whitespaces and       newlines\n " +
        "from a\n" +
        "string\n";

What the String looks like before the removeAll is applied:

This is a string
that streches on several lines.
to demo how to remove whitespaces and newlines
from a
string

Removing the whitespaces is done like this:

withWhiteSpacesString.replaceAll("\\s", "")

And the resulting String is:

Thisisastringthatstrechesonseverallines.todemohowtoremovewhitespacesandnewlinesfromastring

Other usages
  • replacing a small case letter with its counterpart upper case letter. For example, letter ‘a’:
    initialString.replaceAll("a", "A")
  • replacing all digit characters with an whitespace character (space):
    initialString.replaceAll("\\d", " ")

     

  • oh and the basic example of replacing a String with another String, in this case the String “jk” will be replaced by the “_” String:
    complexString.replaceAll("jk", "_");

    You can find all these examples in my GitHub project: https://github.com/iamalittletester/learning-project/blob/master/src/test/java/replaceall/ReplaceAllTest.java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.