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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/classes_objects -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/class_methods -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/modifiers -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/oop -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/identifier -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/arrays -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/data_types -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/operators -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/output -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/strings -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/variables -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/string_methods -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} java/system_methods -.-> lab-117447{{"`Removing Numeric Values from Java Strings`"}} end

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