This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Marcelo Bordolli wrote:i need to grab a Sting like this: "1-25" or one like this "3 selected" and parse it into an int. just the numbers like for the first i need an int = 1 25 or similar; and for the second int = 3;
I've tried doing:
Integer foo = Integer.parseInt("3 selected");
but of course its not working
Can someone advise thanks!!
Basically, the easiest way to find numbers in a string is to use regular expressions... see the java.util.regex.Pattern class. Unfortunately, if you don't know regular expressions, it is probably not a good idea to use it -- although, I do highly recommend that you learn it.
Without regular expression, you can loop through the string looking for the characters (that are digits), and then use the substring() method to extract the numbers to be parsed into a int.