How to extract numbers from a String

This is a simple post on how to extract numbers from a String. I had tried too many methods for the same, and though performance wise this solution cannot be the same, it certainly is the most simple solution that I ever come across. I got this solution  from CodeRanch or StackOverflow (not sure), but thought better to blog so that I can refer it easily later. ..:)

String test = "ABC10DEF20";
test = test.replaceAll("\\D",""); //This will replace all non numeric characters with 
System.out.println("The Number is "+test);

The output of the above snippet of code will be

The Number is 1020

Planning to write more such very small posts that can be used a quick reference.

Happy Working....


Popular posts from this blog

Changing Timezone in Postgresql, Ubuntu

My 10 years in Application Development

Apps to install after installing Ubuntu