Java: removing numeric values from string

user2558595 picture user2558595 · Jul 7, 2013 · Viewed 99.3k times · Source

I have suceeded with the help of this community in removing numeric values from user input, however, my code below will only retrieve the alpha characters before the numeric that has been removed:

import java.util.Scanner;

public class Assignment2_A {

    public static void main(String[] args) {
        Scanner firstname = new Scanner(System.in);
        String firstname1 = firstname.next();
        firstname1 = firstname1.replaceAll("[^A-Z]","");
        System.out.println(firstname1);
    }
}

For example if user input = S1234am, I am only getting back: S. How do I retrieve the remaining characters in the string?

Answer

jlordo picture jlordo · Jul 7, 2013

This will remove all digits:

firstname1 = firstname1.replaceAll("\\d","");