Java - split(regex,limit)方法实际上如何工作? [英] Java - How split(regex, limit) method actually works?

查看:250
本文介绍了Java - split(regex,limit)方法实际上如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to understand how the split method works and have a slight confusion about it. In this Example given in the documentation pages of oracle,

String str = "boo:and:foo";

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

Output
 b
 o:and:foo

这很容易理解,字符串在第一个'o'

This is easy to understand, that the string has been literally divided at the occurence of the first 'o'

的出现时实际上是分开的,但对于

but for

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

Output:
b

:and:foo 

这是怎么出现的?

推荐答案

我从文档中理解:


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

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.

这意味着在字符串s上设定或减少n次,所以让我们逐一分析以更好地理解:

This mean devise or cut it to n time on string s, so Lets analyse one by one to understand better :

限制1

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

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

This mean split it or cut it on just one string on the string o in this case you will get all your input :

[boo:and:foo]
 1

限制2

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

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

Which mean cut it one time on o so i will put a break in the first o

    boo:and:foo
-----^

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

in this case you will get two results :

[b,o:and:foo]
 1 2

限制3

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

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

Which mean cut it two times on the first o and on the second o

    boo:and:foo
1----^^--------------2

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

in this case you will get three results :

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

限制4

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

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

Which mean cut it three times on the first, second and third o

     boo:and:foo
1_____^^      ^
       |___2  |___3

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

in this case you will get four results :

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

限价5

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

这意味着第一次,第二次,第三次和第四次减去四次 o

Which mean cut it four times on first, second, third and forth o

     boo:and:foo
1_____^^      ^^
       |___2  ||___4
              |____3

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

in this case you will get five results :

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






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


Just a simple animation to understand more :

这篇关于Java - split(regex,limit)方法实际上如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆