Removing Numeric Values from Java Strings

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to remove numeric values from a String in Java. Strings in Java can contain alphanumeric characters and special symbols. Therefore, sometimes it is necessary to filter the String to get only the alphabets. In this lab, we will use the replaceAll() method, which takes two arguments: a regular expression to filter the String and a replacement String.

Create a new Java file

Navigate to the ~/project directory and create a new Java file using the following command:

touch RemoveNumericValues.java

Define the main() method

Open the RemoveNumericValues.java file in a text editor and define the main() method as follows:

public class RemoveNumericValues {
    public static void main(String[] args) {
        String str = "House123sector4";
        System.out.println("Original String: " + str);
    }
}

Use regex to remove numeric values

In this step, we will use Regex to remove the numeric values from the String.

public class RemoveNumericValues {
    public static void main(String[] args) {
        String str = "House123sector4";
        System.out.println("Original String: " + str);

        str = str.replaceAll("[^A-Za-z]", "");
        System.out.println("String with only Alphabets: " + str);
    }
}

In the above code, we have used the replaceAll() method to replace all characters except the alphabets from the String str. The regex [A-Za-z] will match all characters from A to Z and a to z.

Use regex to remove digits

In this step, we will use Regex to remove the digits from the String.

public class RemoveNumericValues {
    public static void main(String[] args) {
        String str = "House123sector4";
        System.out.println("Original String: " + str);

        str = str.replaceAll("\\d", "");
        System.out.println("String with only Alphabets: " + str);
    }
}

In the above code, we have used the replaceAll() method to replace all digits from the String str. The regex \\d will match all the digits present in the String.

Use regex to remove digits (alternative method)

In this step, we will use another Regex to remove the digits from the String.

public class RemoveNumericValues {
    public static void main(String[] args) {
        String str = "House123sector4";
        System.out.println("Original String: " + str);

        str = str.replaceAll("[0-9]", "");
        System.out.println("String with only Alphabets: " + str);
    }
}

In the above code, we have used the replaceAll() method to replace all digits from the String str. The regex [0-9] will match all the digits present in the String.

Compile and run the code

Save the changes made to the RemoveNumericValues.java file, open the terminal, navigate to the project directory and enter the following command to compile the code:

javac RemoveNumericValues.java

After the code has been compiled successfully, enter the following command to run the code:

java RemoveNumericValues

The output of the code will be displayed on the terminal.

Modify the input String

In order to test the code on different input strings, modify the str String to any value of your choice.

public class RemoveNumericValues {
    public static void main(String[] args) {
        String str = "Hello%&$3World";
        System.out.println("Original String: " + str);

        str = str.replaceAll("[^A-Za-z]", "");
        System.out.println("String with only Alphabets: " + str);
    }
}

Test the alternative method

Modify the previous version of the code to test the alternative method as follows:

public class RemoveNumericValues {
    public static void main(String[] args) {
        String str = "Hello%&$3World";
        System.out.println("Original String: " + str);

        str = str.replaceAll("[0-9]", "");
        System.out.println("String with only Alphabets: " + str);
    }
}

Test the second method

Modify the previous version of the code to test the second method as follows:

public class RemoveNumericValues {
    public static void main(String[] args) {
        String str = "Hello%&$3World";
        System.out.println("Original String: " + str);

        str = str.replaceAll("\\d", "");
        System.out.println("String with only Alphabets: " + str);
    }
}

Summary

In this lab, we have learned how to remove numeric values from a String in Java. We have used the replaceAll() method along with different Regex to filter the String and get only the alphabets. We have also learned how to test the code with different input strings.

Other Java Tutorials you may like