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?
Java code to extract digits or numbers from a string with logic and explanation. We'll use 2 methods: (1) using regex expression with replaceAll method of String object, and (2) using loop and ascii values of characters.
isDigit(s. charAt(i)) will count the number of digits in the given string. 'Character. isDigit' will count the number of each digit in the string.
You can use regex and delete non-digits.
str = str.replaceAll("\\D+","");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With