kooyee ‘s blog

          開源軟件, 眾人努力的結(jié)晶, 全人類的共同財(cái)富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          [Java.lang]String.split用法

          Posted on 2007-07-17 23:29 kooyee 閱讀(1550) 評(píng)論(0)  編輯  收藏 所屬分類: Java

          public String[] split(String regex, int limit)

          limit n 大于0,則pattern(模式)應(yīng)用n - 1 次

          String s = "boo:and:foo"
          s.split(
          ":",2)
          //result is { "boo", "and:foo" }

          limit n 小于0,則pattern(模式)應(yīng)用無限次

          String s = "boo:and:foo"
          s.split(
          ":",-2)
          //result is { "boo", "and", "foo" }

          limit n 等于0,則pattern(模式)應(yīng)用無限次并且省略末尾的空字串

          String s = "boo:and:foo"
          s.split("o", -2)
             //result is { "b", "", "and:f", "", "" }
             s.split(
          "o", 0)
             //result is { "b", "", "and:f" }




          The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

          The string "boo:and:foo", for example, yields the following results with these parameters:

          Regex Limit Result
          : 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" }

          主站蜘蛛池模板: 涪陵区| 宜兰市| 汪清县| 金沙县| 白水县| 奉贤区| 裕民县| 梅河口市| 桂阳县| 澄迈县| 临城县| 呼和浩特市| 千阳县| 邮箱| 安泽县| 略阳县| 广宁县| 理塘县| 钦州市| 淅川县| 开封县| 桦甸市| 临城县| 新化县| 秦皇岛市| 六枝特区| 嵊泗县| 长顺县| 杭锦旗| 廉江市| 黄平县| 南投市| 肇州县| 邢台市| 额尔古纳市| 雷州市| 张家界市| 千阳县| 类乌齐县| 德清县| 洪江市|