public class Test { public static String convert(String str) { String[] arrStr = str.split(","); String result = ""; for (int i = 0; i < arrStr.length; i++) { if (arrStr[i].indexOf("-") >= 0) { Integer begin = new Integer(arrStr[i].substring(0, arrStr[i].indexOf("-"))); Integer end = new Integer(arrStr[i].substring(arrStr[i].indexOf("-")+1)); for (int j = begin.intValue(); j < end.intValue() + 1; j++) { result = result + "," + new Integer(j); } } else { result = result + "," + arrStr[i]; } } if (!result.equals("")) { result = result.substring(1, result.length()); } return result; } public static void main(String[] args) { String str="123,125-127,167-198"; System.out.println(Test.convert(str)); } }
String[] arrStr = str.split(",");
String result = "";
for (int i = 0; i < arrStr.length; i++) {
if (arrStr[i].indexOf("-") >= 0) {
Integer begin = new Integer(arrStr[i].substring(0, arrStr[i].indexOf("-")));
Integer end = new Integer(arrStr[i].substring(arrStr[i].indexOf("-")+1)); for (int j = begin.intValue(); j < end.intValue() + 1; j++) {
result = result + "," + new Integer(j);
}
} else {
result = result + "," + arrStr[i];
}
} if (!result.equals("")) {
result = result.substring(1, result.length());
} return result;
} public static void main(String[] args) {
String str="123,125-127,167-198";
System.out.println(Test.convert(str));
}
}