0

How extract number from string .

for ex: "hello123hi" from this string i want only the numbers. 123..

2nd Sep 2018, 6:48 AM
Karankumar
3 Answers
+ 3
String input = "hello123hi"; String numbers = ""; for(int i=0;i<input.length();i++){ if(Character.isDigit(input.charAt(i))){ numbers += input.charAt(i); } } System.out.print(numbers);
2nd Sep 2018, 7:10 AM
JME
+ 2
Very similar, but using for each: for (char c : input.toCharArray()) { if (Character.isDigit(c)) { numbers += c; } }
2nd Sep 2018, 7:30 AM
Dan Walker
Dan Walker - avatar
+ 2
The examples given here are working great. However if you find that you need to extract pattern out of the given string, regular expression (Regex) would always be the most efficient choice. 😉
2nd Sep 2018, 9:59 AM
Zephyr Koo
Zephyr Koo - avatar