<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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)  編輯  收藏

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


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 亚洲美女视频一区二区三区| 亚洲一区二区三区无码中文字幕 | 亚洲国产精华液2020| 亚洲精品在线免费看| 亚洲成人高清在线观看| 麻花传媒剧在线mv免费观看| 亚洲视频免费一区| 四虎永久在线观看免费网站网址| 亚洲最大黄色网站| 岛国av无码免费无禁网站| 亚洲欧美日韩一区二区三区| 国产免费人成视频在线观看| 黄网站色成年片大免费高清| 亚洲国产一区二区三区| 久久九九久精品国产免费直播| 精品亚洲综合在线第一区| 99在线观看精品免费99| 亚洲一区二区三区四区视频| 在线播放免费人成视频在线观看| 国产精品国产亚洲区艳妇糸列短篇| 免费人成在线观看视频播放| 久久久精品视频免费观看| 亚洲国产成人高清在线观看 | 成人免费观看男女羞羞视频| 亚洲精品国产品国语在线| 久久99国产综合精品免费| 亚洲精品国产av成拍色拍| 国产a v无码专区亚洲av| 久草福利资源网站免费| 亚洲精品中文字幕无乱码麻豆| 免费在线视频一区| 日本免费久久久久久久网站| 97久久国产亚洲精品超碰热| 免费a级毛片18以上观看精品| 中国国产高清免费av片| 亚洲xxxxxx| 亚洲小说区图片区另类春色| 黄+色+性+人免费| 无码毛片一区二区三区视频免费播放 | 亚洲人成精品久久久久| 和日本免费不卡在线v|