Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, replace string numbers with blankstring and remove everything after the numbers

I have strings like:

Alian 12WE 

and

ANI1451

Is there any way to replace all the numbers (and everything after the numbers) with an empty string in JAVA?

I want the output to look like this:

Alian

ANI
like image 252
user1966221 Avatar asked Mar 09 '13 17:03

user1966221


1 Answers

With a regex, it's pretty simple:

public class Test {

    public static String replaceAll(String string) {
        return string.replaceAll("\\d+.*", "");
    }

    public static void main(String[] args) {
        System.out.println(replaceAll("Alian 12WE"));
        System.out.println(replaceAll("ANI1451"));
    }   
}
like image 168
1218985 Avatar answered Oct 24 '22 18:10

1218985