有字符串a="123,125-127",其中'125-127'代表连续号'125,126,127',如何将"123,125-127"分解成"123,125,126,127"??

解决方案 »

  1.   

    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));
      }
    }
      

  2.   

    关键是for (int j = begin.intValue(); j < end.intValue() + 1; j++)