new Scanner(str).useDelimiter("[^\\d]+").nextInt()
You can use next() instead of nextInt() to get the digits as a String. Note that calling Integer.parseInt on the result may be many times faster than calling nextInt().
You can check for the presence of number using hasNextInt() on the Scanner.
I've created a JUnit Test class(as a additional knowledge/info) for the same issue. Hope you'll be finding this helpful.
public class StringHelper {
//Separate words from String which has gigits
public String drawDigitsFromString(String strValue){
String str = strValue.trim();
String digits="";
for (int i = 0; i < str.length(); i++) {
char chrs = str.charAt(i);
if (Character.isDigit(chrs))
digits = digits+chrs;
}
return digits;
}
}
And JUnit Test case is:
public class StringHelperTest {
StringHelper helper;
@Before
public void before(){
helper = new StringHelper();
}
@Test
public void testDrawDigitsFromString(){
assertEquals("187111", helper.drawDigitsFromString("TCS187TCS111"));
}
}
`String s="as234dfd423";
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);``
char d=s.charAt(i);
if ('a' <= c && c <= 'z')
System.out.println("String:-"+c);
else if ('0' <= d && d <= '9')
System.out.println("number:-"+d);
}