0
How extract number from string .
for ex: "hello123hi" from this string i want only the numbers. 123..
4 Respostas
+ 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);
+ 2
Very similar, but using for each:
for (char c : input.toCharArray()) {
if (Character.isDigit(c)) {
numbers += c;
}
}
+ 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. 😉