Reputation:
I have my below string as
String version = "v2";
here version can be any of these values -
v3
v4
v5
..
v10
v11
..
v100
..
v500
..
v1000
I would like to extract number from the above string so which could be 2, 3, 4, 5, 10, 11, 100, 500, 1000
from it.
What is the correct way to extract only the number from it?
Upvotes: 0
Views: 134
Reputation: 1882
You can use regex:
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(your string);
while (m.find()) {
int n = Integer.parseInt(m.group());
Upvotes: 1
Reputation: 178263
If the first character is always 'v'
, then remove it from the string, then call Integer.parseInt
:
int n = Integer.parseInt(version.substring(1));
Upvotes: 7