Extract Integer Part in String

Verhogen picture Verhogen · Dec 14, 2009 · Viewed 94.5k times · Source

What is the best way to extract the integer part of a string like

Hello123

How do you get the 123 part. You can sort of hack it using Java's Scanner, is there a better way?

Answer

Ascalonian picture Ascalonian · Dec 14, 2009

As explained before, try using Regular Expressions. This should help out:

String value = "Hello123";
String intValue = value.replaceAll("[^0-9]", ""); // returns 123

And then you just convert that to an int (or Integer) from there.