How do I remove the non-numeric character from a string in java? [closed]

Are you removing or splitting? This will remove all the non-numeric characters.

myStr = myStr.replaceAll( "[^\\d]", "" )

One more approach for removing all non-numeric characters from a string:

String newString = oldString.replaceAll("[^0-9]", "");

String str= "somestring";
String[] values = str.split("\\D+"); 

Another regex solution:

string.replace(/\D/g,'');  //remove the non-Numeric

Similarly, you can

string.replace(/\W/g,'');  //remove the non-alphaNumeric

In RegEX, the symbol '\' would make the letter following it a template: \w -- alphanumeric, and \W - Non-AlphaNumeric, negates when you capitalize the letter.


You will want to use the String class' Split() method and pass in a regular expression of "\D+" which will match at least one non-number.

myString.split("\\D+");