隨筆-9  評論-15  文章-1  trackbacks-0
          在工作中碰到ip地址檢查的問題,因此自己寫了一個checkIPAdress方法。
           1/**
           2   * check input is ip or not.
           3   * @return a strResult.1 is not ip adress,0 is ipadress
           4   */

           5
           6  private String checkIPAdress(String input) {
           7
           8    String strResult = "0";
           9    //identify the length of the string
          10    if (input.length() < 7 || input.length() > 15{
          11      return "1";
          12    }

          13
          14    int startIndex = 0;
          15    Vector v = new Vector();
          16    int index = 0;
          17    startIndex = input.indexOf(".");
          18    while (startIndex < input.length() && startIndex != -1{
          19      String temp = input.substring(index, startIndex);
          20      v.addElement(temp);
          21      index = startIndex + 1;
          22      startIndex = input.indexOf(".", startIndex + 1);
          23    }

          24    v.addElement(input.substring(index));
          25    if (v.size() != 4{
          26      return "1";
          27    }

          28    for (int i = 0; i < v.size(); i++{
          29      try {
          30        int iTemp = Integer.parseInt( (String) v.elementAt(i));
          31
          32        if (iTemp < 0 || iTemp > 254{
          33
          34          return "1";
          35        }

          36
          37      }

          38      catch (Exception e) {
          39        e.printStackTrace();
          40        return "1";
          41      }

          42
          43    }

          44
          45    return strResult;
          46
          47  }
               起始在判斷分割ip地址應該使用split方法。但是因為我系統(tǒng)中用的是jdk1.2所以采用這個笨方法,如果覺得不爽可以改成split,更方便。
          posted on 2005-08-11 12:50 jam 閱讀(489) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 安远县| 施秉县| 高唐县| 泰宁县| 镇康县| 托克托县| 洞头县| 雷山县| 化隆| 北宁市| 子洲县| 泽库县| 苏尼特左旗| 固阳县| 仙游县| 江西省| 河曲县| 东港市| 康乐县| 海晏县| 咸宁市| 蒙阴县| 奉化市| 太谷县| 济源市| 剑河县| 广元市| 象州县| 遂昌县| 炉霍县| 西丰县| 紫金县| 阿拉善左旗| 仪陇县| 文成县| 东明县| 萨嘎县| 永寿县| 石柱| 临猗县| 锦州市|