user488469 user488469 - 1 year ago 117
Java Question

Extract digits from a string in Java

I have a Java

object. I need to extract only digits from it. I'll give an example:

I want

Is there a library function that extracts only digits?

Thanks for the answers. Before I try these I need to know if I have to install any additional llibraries?

Answer Source

You can use regex and delete non-digits.

str = str.replaceAll("\\D+","");