posts - 241,  comments - 116,  trackbacks - 0
          Java split用法

          java.lang.string.split

          split 方法
          將一個字符串分割為子字符串,然后將結果作為字符串數組返回。

          stringObj.split([separator,[limit]])

          stringObj
          必選項。要被分解的 String 對象或文字。該對象不會被 split 方法修改。

          separator
          可選項。字符串或 正則表達式 對象,它標識了分隔字符串時使用的是一個還是多個字符。如果忽
          略該選項,返回包含整個字符串的單一元素數組。

          limit
          可選項。該值用來限制返回數組中的元素個數。

          說明:
          split 方法的結果是一個字符串數組,在 stingObj 中每個出現 separator 的位置都要進行分解
          。separator 不作為任何數組元素的部分返回。


          示例1:
          public class SplitDemo {
             
               public static String[] ss = new String[20];

               public SplitDemo() {

                   String s = "The rain in Spain falls mainly in the plain.";
                   // 在每個空格字符處進行分解。 風之境地
                   ss = s.split(" ");
               }

               public static void main(String[] args) {

                   SplitDemo demo = new SplitDemo();
                   for (int i = 0; i < ss.length; i++)
                       System.out.println(ss[i]);
               }

          }

          程序結果:
          The
          rain
          in
          Spain
          falls
          mainly
          in
          the
          plain.


          示例2:
          public class SplitDemo {

               public static String[] ss = new String[20];

               public SplitDemo() {

                   String s = "The rain in Spain falls mainly in the plain.";
                   // 在每個空格字符處進行分解。
                   ss = s.split(" ", 2);
               }

               public static void main(String[] args) {
                   SplitDemo demo = new SplitDemo();
                   for (int i = 0; i < ss.length; i++)
                       System.out.println(ss[i]);
               }

          }

          程序結果:
          The
          rain in Spain falls mainly in the plain.


          示例3:
          public class SplitDemo {

               public static String[] ss = new String[20];

               public SplitDemo() {

                   String s = "The rain in Spain falls mainly in the plain.";
                   // 在每個空格字符處進行分解。
                   ss = s.split(" ", 20);
               }

               public static void main(String[] args) {
                   SplitDemo demo = new SplitDemo();
                   for (int i = 0; i < ss.length; i++)
                       System.out.println(ss[i]);
               }

          }

          程序結果:
          The
          rain
          in
          Spain
          falls
          mainly
          in
          the
          plain.


          示例4:
          public class SplitDemo {

               public static void main(String[] args) {

                   String value = "192.168.128.33";
                   String[] names = value.split(".");
                   for (int i = 0; i < names.length; i++) {
                       System.out.println(names[i]);
                   }

               }
          }

          運行結果:

          對,沒看錯!沒有任何輸出!
          讓我們來看看 split 方法的方法簽名吧:

          public string[] split(string regex)
          這里的參數的名稱是 regex ,也就是 regular expression (正則表達式)。這個參數并不是一個簡單的分割用的字符,而是一個正則表達式,看了 split 方法的實現代碼就更堅定了我們的信心:

          public string[] split(string regex, int limit) {
          return pattern.compile(regex).split(this, limit);
          }
          split 的實現直接調用的 matcher 類的 split 的方法。讀者已經知道,“ . ”在正則表達式中有特殊的含義,因此我們使用的時候必須進行轉義。
          只要將
          String[] names = value.split(".");
          改為
          String[] names = value.split("\\.");
          就可以了。

          輸出結果:
          192
          168
          128
          33


          再加一點兒補充(這是Java幫助文檔中的,更清晰一些):

          public String[] split(String regex,int limit)根據匹配給定的正則表達式來拆分此字符串。
          此方法返回的數組包含此字符串的每個子字符串,這些子字符串由另一個匹配給定的表達式的子字符串終止或由字符串結束來終止。數組中的子字符串按它們在此字符串中的順序排列。如果表達式不匹配輸入的任何部分,則結果數組只具有一個元素,即此字符串。

          limit 參數控制模式應用的次數,因此影響結果數組的長度。如果該限制 n 大于 0,則模式將被最多應用 n - 1 次,數組的長度將不會大于 n,而且數組的最后項將包含超出最后匹配的定界符的所有輸入。如果 n 為非正,則模式將被應用盡可能多的次數,而且數組可以是任意長度。如果 n 為零,則模式將被應用盡可能多的次數,數組可有任何長度,并且結尾空字符串將被丟棄。

          例如,字符串 "boo:and:foo" 使用這些參數可生成下列結果:

          Regex      Limit                結果

             :          2             { "boo", "and:foo" }
             :          5             { "boo", "and", "foo" }
             :          -2            { "boo", "and", "foo" }
             o          5             { "b", "", ":and:f", "", "" }
             o          -2            { "b", "", ":and:f", "", "" }
             o          0             { "b", "", ":and:f" }

          這種形式的方法調用 str.split(regex, n) 產生與以下表達式完全相同的結果:

          Pattern.compile(regex).split(str, n)

          參數:
          regex - 定界正則表達式
          limit - 結果閾值,如上所述
          返回:
          字符串數組,根據給定正則表達式的匹配來拆分此字符串,從而生成此數組
          拋出:
          PatternSyntaxException - 如果正則表達式的語法無效
          從以下版本開始:
          1.4


          public String[] split(String regex)根據給定的正則表達式的匹配來拆分此字符串。
          該方法的作用就像是使用給定的表達式和限制參數 0 來調用兩參數 split 方法。因此,結果數組中不包括結尾空字符串。

          例如,字符串 "boo:and:foo" 產生帶有下面這些表達式的結果:

          Regex                 結果
             :            { "boo", "and", "foo" }
             o            { "b", "", ":and:f" }

          參數:
          regex - 定界正則表達式
          返回:
          字符串數組,根據給定正則表達式的匹配來拆分此字符串,從而生成此數組。
          拋出:
          PatternSyntaxException - 如果正則表達式的語法無效
          posted on 2011-10-13 10:35 墻頭草 閱讀(283) 評論(0)  編輯  收藏

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


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 平舆县| 伊吾县| 澄迈县| 三明市| 治多县| 金门县| 高州市| 阿图什市| 顺平县| 松溪县| 中卫市| 福泉市| 嵊泗县| 阿图什市| 延津县| 原平市| 琼海市| 井冈山市| 观塘区| 进贤县| 伊川县| 门源| 阿城市| 垫江县| 新巴尔虎右旗| 都安| 元江| 日照市| 册亨县| 类乌齐县| 台北县| 江都市| 淮北市| 甘南县| 乌拉特后旗| 通许县| 鹤岗市| 文化| 华亭县| 祁东县| 都昌县|