Java - split(regex,limit)方法实际上如何工作?

时间:2017-11-29 12:19:01

标签: java string split

我试图了解拆分方法是如何工作的,并且对它有轻微的混淆。在oracle的文档页面中给出的这个例子中,

String str = "boo:and:foo";

String[] str1 = str.split("o",2);

Output
 b
 o:and:foo

这很容易理解,字符串在第一个' o'出现的情况下实际上是分开的。

但是

String[] str1 = str.split("o",3);

Output:
b

:and:foo 

这是怎么出现的?

7 个答案:

答案 0 :(得分:4)

我从文档中了解到:

  

limit参数控制模式的次数   应用因此会影响结果数组的长度。 如果   限制n大于零,那么模式将应用于   大多数n - 1次,数组的长度不会大于n,并且   数组的最后一个条目将包含除最后一个匹配之外的所有输入   定界符即可。如果n是非正数,那么该模式将被应用为   尽可能多次,阵列可以有任何长度。如果n为零   那么模式将尽可能多地应用于数组   可以有任何长度,尾随空字符串将被丢弃。

这意味着在字符串s上设计或剪切到n次,所以让我们逐一分析以便更好地理解:

限制1

String[] spl1 = str.split("o", 1);

这意味着将它分割或剪切在字符串o上的一个字符串上,在这种情况下,您将获得所有输入:

[boo:and:foo]
 1

限制2

String[] spl1 = str.split("o", 2);

这意味着在o上减少一次,所以我会在第一个o

中休息一下
    boo:and:foo
-----^

在这种情况下,您将得到两个结果:

[b,o:and:foo]
 1 2

限制3

String[] spl1 = str.split("o", 3);

这意味着在第一个o和第二个o

上将其剪切两次
    boo:and:foo
1----^^--------------2

在这种情况下,您将得到三个结果:

[b, ,:and:foo]
 1 2  3

限制4

String[] spl1 = str.split("o", 4);

这意味着在第一,第二和第三o

上将其削减三次
     boo:and:foo
1_____^^      ^
       |___2  |___3

在这种情况下,您将得到四个结果:

[b, ,:and:f,o]
 1 2 3      4

限制5

String[] spl1 = str.split("o", 5);

这意味着在第一,第二,第三和第四o

上将其削减四次
     boo:and:foo
1_____^^      ^^
       |___2  ||___4
              |____3

在这种情况下,您将得到五个结果:

[b, ,:and:f, , ]
 1 2  3     4 5

只是一个简单的动画来了解更多:

How split() method actually works?

答案 1 :(得分:1)

第二个参数表示模式需要应用的次数。

来自Java Docs:

  

limit参数控制模式的次数   应用因此会影响结果数组的长度。如果   限制n大于零,那么模式将应用于   大多数n - 1次,数组的长度不会大于n,并且   数组的最后一个条目将包含除最后一个匹配之外的所有输入   分隔符。如果n是非正数,那么该模式将被应用为   尽可能多次,阵列可以有任何长度。如果n为零   那么模式将尽可能多地应用于数组   可以有任何长度,尾随空字符串将被丢弃。

示例:

1)如果限制设置为零(str.split(“o”,0))那么根据java文档,模式将被应用尽可能多次,因此结果将是:

  

[b ,,:和:f]

2)但如果您将限制设置为非零值(例如1或2),则模式将应用n-1次(例如,对于限制1模式将应用0次,对于2将应用1次)所以结果如下:

  

[boo:and:foo] //为str.split(“o”,1)应用了0次。

     

[b,o:和:foo] //为str.split(“o”,2)应用了1次。

     

[b ,,::和foo] //为str.split(“o”,3)应用2次。

答案 2 :(得分:0)

第二个参数是正则表达式应用于字符串的次数。 因此,如果限制为3,您将获得b,,:and:foo:在模式出现的位置将字符串拆分为令牌。 请注意,限制可能大于实际字符串中正则表达式的出现次数。

答案 3 :(得分:0)

String[] split(String regex, int limit)

第二个参数限制分割后返回的字符串数。

例如split("anydelimiter", 3)只返回3个字符串的数组,即使字符串中的分隔符超过3次。

如果限制为负,则返回的数组将具有尽可能多的子字符串,但是当限制为零时,返回的数组将具有除尾随空字符串之外的所有子字符串。

答案 4 :(得分:0)

两个参数Text()的方法请求 split(String regex, int limit) - 分隔正则表达式;
regex - 结果阈值。

Java documentation

答案 5 :(得分:0)

**regex** − the delimiting regular expression

  String Str = new String("boo:and:foo:com:boo");
 System.out.println("Return value1: ");
        for (String retval : Str.split(":", 2)) {
            System.out.println(retval);
        }
        System.out.println();
        System.out.println("Return value2: ");
        for (String retval : Str.split(":", 3)) {
            System.out.println(retval);
        }
        System.out.println();
        System.out.println("Return value3: ");
        for (String retval : Str.split(":", 0)) {
            System.out.println(retval);
        }
        System.out.println();
        System.out.println("Return value4: ");
        for (String retval : Str.split(":")) {
            System.out.println(retval);
        }
    }

你自己测试每个值1,2,3,4

答案 6 :(得分:0)

这是限制

的文档
<p> The <tt>limit</tt> parameter controls the number of times the
     * pattern is applied and therefore affects the length of the resulting
     * array.  If the limit <i>n</i> is greater than zero then the pattern
     * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
     * length will be no greater than <i>n</i>, and the array's last entry
     * will contain all input beyond the last matched delimiter.  If <i>n</i>
     * is non-positive then the pattern will be applied as many times as
     * possible and the array can have any length.  If <i>n</i> 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.

和String类的split方法的源代码。

 public String[] split(String regex, int limit) {
        /* fastpath if the regex is a
         (1)one-char String and this character is not one of the
            RegEx's meta characters ".$|()[{^?*+\\", or
         (2)two-char String and the first char is the backslash and
            the second is not the ascii digit or ascii letter.
         */
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            if (off == 0)
                return new String[]{this};

            // Add remaining segment
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));

            // Construct result
            int resultSize = list.size();
            if (limit == 0)
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
                    resultSize--;
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        return Pattern.compile(regex).split(this, limit);
    }

查看此部分代码

            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }

结果列表大小将达到极限。