Get int from String, also containing letters, in Java

integer, java

Solution

Replace all non-digit with blank: the remaining string contains only digits.

Integer.parseInt(s.replaceAll("[\\D]", ""))

This will also remove non-digits inbetween digits, so `"x1x1x"` becomes `11`.

If you need to confirm that the string consists of a sequence of digits (at least one) possibly followed a letter, then use this:

s.matches("[\\d]+[A-Za-z]?")

Problem

How can I get the int value from a string such as `423e` - i.e. a string that contains a number but also maybe a letter? `Integer.parseInt()` fails since the string must be entirely a number.

Original source