Hi! I need function that will take string and integer that indicates position of non-negative double or integer and return Number or null. If there is '+' return null.
Examples
2.1 , 0 -> 2.1
+2.1 , 0 -> null
-1 , 0 -> null
-1.2 , 1 -> 1.2
qwa56sdf , 3 -> 56
What is the most elegant way to do this? Thanks.
upd I need code like this, but better)
Number parse(String str, int pos){
Matcher m = Pattern.compile("^(\\d+(?:\\.\\d+)?)").matcher(str);
m.region(pos, str.length());
if(m.find()){
return Double.parseDouble(m.group());
} else {
return null;
}
}