为什么String.split以不同的方式处理字符串的开头和结尾部分?

Joh*_*ood 2 java string split

例:

String s = ":a:b:c:";
s.split(":");
// Output: [, a, b, c]
Run Code Online (Sandbox Code Playgroud)

来自Java Doc:

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

为什么起始空字符串被认为是结束空字符串不在哪里?起始空字符串以":"结尾,结束空字符串以字符串结尾终止.所以两者都应该列出,如果不是的话?

rge*_*man 6

如果不提供a limit,则该split方法不会返回尾随空匹配.引用Javadocs:

将此字符串拆分为给定正则表达式的匹配项.此方法的工作方式就像调用带有给定表达式和limit参数为零的双参数split方法一样.因此,结尾的空字符串不包含在结果数组中.

如果使用2参数版本split,则传入负限制,并且不会限制返回数组的大小; 你会得到尾随空字符串.

如果n是非正数,那么模式将被应用尽可能多的次数,并且数组可以具有任何长度.

在这里,我认为Javadocs limit在他们说什么时指的是n.