Is there a simple way to parse the following String to List of Integers:
String s = \"1,2,5-9,11,12\"
// expected values list: [1,2,5,6,7,8,9,11,12]
List
Magic you are looking at
public static List magicallyGetListFromString(String s){
String[] str=s.split(",");
List result=new ArrayList();
for(String i:str){
if(i.contains("-")){
String[] ss=i.split("-");
int lower= Integer.parseInt(ss[0]);
int upper= Integer.parseInt(ss[1]);
for(int j=lower;j<=upper;j++){
result.add(j);
}
}else {
result.add(Integer.parseInt(i));
}
}
return result;
}
Result:
[1, 2, 5, 6, 7, 8, 9, 11, 12]