Ranjith K

Jan 3, 2021

1 min read

How to get int from String, also containing letters using Java?

To get only integer from a string which has both letters and numbers in java use the following method

Integer.parseInt(s.replaceAll("[\\D]", ""))
If you copy paste this code in android studio 2 slash before D will be converted into 4 slash so it won’t replace letters in the string by empty character. So just remove 2 slash before D. Because while checking this in my project I copied the code and face this issue.I don’t have an idea about other IDE.