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

          java.lang.string.split

          split 方法
          將一個字符串分割為子字符串,然后將結(jié)果作為字符串?dāng)?shù)組返回。

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

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

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

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

          說明:
          split 方法的結(jié)果是一個字符串?dāng)?shù)組,在 stingObj 中每個出現(xiàn) separator 的位置都要進行分解
          。separator 不作為任何數(shù)組元素的部分返回。


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

               public SplitDemo() {

                   String s = "The rain in Spain falls mainly in the plain.";
                   // 在每個空格字符處進行分解。 風(fēng)之境地
                   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]);
               }

          }

          程序結(jié)果:
          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]);
               }

          }

          程序結(jié)果:
          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]);
               }

          }

          程序結(jié)果:
          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]);
                   }

               }
          }

          運行結(jié)果:

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

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

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

          輸出結(jié)果:
          192
          168
          128
          33


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

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

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

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

          Regex      Limit                結(jié)果

             :          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" }

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

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

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


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

          例如,字符串 "boo:and:foo" 產(chǎn)生帶有下面這些表達式的結(jié)果:

          Regex                 結(jié)果
             :            { "boo", "and", "foo" }
             o            { "b", "", ":and:f" }

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

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


          網(wǎng)站導(dǎo)航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運專家
          主站蜘蛛池模板: 巩留县| 石门县| 上栗县| 洛川县| 丹东市| 韶山市| 四子王旗| 寻乌县| 徐州市| 丰镇市| 汪清县| 腾冲县| 瑞昌市| 太保市| 漯河市| 达拉特旗| 普格县| 镇沅| 婺源县| 阿克苏市| 安达市| 曲靖市| 宝山区| 北安市| 瑞昌市| 射阳县| 新乡县| 石首市| 天津市| 清徐县| 台安县| 河北省| 九台市| 苏尼特右旗| 崇阳县| 扎鲁特旗| 江达县| 汝城县| 交口县| 揭阳市| 溧阳市|