Hello!
Question: how to use a regular expression in java to extract a number from a given string?
0-30: 250
Interested in the number after the colon.
The number can contain more than three characters.
Hello!
Question: how to use a regular expression in java to extract a number from a given string?
0-30: 250
Interested in the number after the colon.
The number can contain more than three characters.
String string = "0-30 : 250"; String[] parts = string.split(":"); int myNum = Integer.parseInt(parts[1].trim()); System.out.print(myNum); The split () method splits a string around the specified regular expression. In this case, just by the colon.
Then we remove the same number from the second part of the resulting array, preliminarily cutting the spaces (trim). And convert it to a number via Integer.parseInt
Source: https://ru.stackoverflow.com/questions/595461/
All Articles