Java String remove all non numeric characters

DRing picture DRing · Apr 29, 2012 · Viewed 305.8k times · Source

Trying to remove all letters and characters that are not 0-9 and a period. I'm using Character.isDigit() but it also removes decimal, how can I also keep the decimal?

Answer

Óscar López picture Óscar López · Apr 29, 2012

Try this code:

String str = "a12.334tyz.78x";
str = str.replaceAll("[^\\d.]", "");

Now str will contain "12.334.78".