user488469 user488469 - 2 months ago 8
Java Question

Extract digits from a string in Java

I have a Java

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

"123-456-789"
I want
"123456789"


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

You can use regex and delete non-digits.

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