简体   繁体   English

执行此split()函数时为什么java与javascript不同

[英]Why java is different from javascript when execute this split() function

In javascript 在javascript中

",,".split(",").length   // Return 3

But in java: 但在java中:

",,".split(",").length   // Return 0

Why java ignores all empty string in between when split string with charater? 为什么java在使用charater拆分字符串时忽略之间的所有空字符串?

Java are having two overloading functions of String.split(..) Java有两个重载函数String.split(..)

 1. public String[] split(String regex, int limit){}
 2. public String[] split(String regex) . This equals with split(String regex, int limit = 0)

With limit parameter make String.spit of java flexible. 使用limit参数make java的String.spit灵活。 The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. limit参数控制模式的应用次数,因此会影响结果数组的长度。 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. 如果限制n大于零,那么模式将最多应用n - 1次,数组的长度将不大于n,并且数组的最后一个条目将包含超出最后匹配分隔符的所有输入。 If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. 如果n是非正数,那么模式将被应用尽可能多的次数,并且数组可以具有任何长度。 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. 如果n为零,那么模式将被应用尽可能多的次数,该数组可以具有任何长度,并且将丢弃尾随的空字符串。 More detail here 更多细节在这里

So in your case with java: 所以在你的情况下使用java:

",,".split(",").length   // Return 0
",,".split(",", -1).length   // Return 3

To know why there is difference you will have to understand split(String regex, int limit) first. 要知道为什么存在差异,首先必须了解split(String regex, int limit)

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. limit参数控制模式的应用次数,因此会影响结果数组的长度。 And they specifically says 他们特意说

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. 如果n为零,那么模式将被应用尽可能多的次数,该数组可以具有任何长度,并且将丢弃尾随的空字符串。

java docs java文档

Now in split(String regex) . 现在在split(String regex) This overloaded function is supposed to give the same result as above with a limit argument of zero. 这个重载函数应该给出与上面相同的结果,其limit参数为零。

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. 此方法的作用就像通过调用具有给定表达式和limit参数为零的双参数split方法一样。 Trailing empty strings are therefore not included in the resulting array. 因此,结尾的空字符串不包含在结果数组中。

Now on the other hand when we talk about javascript. 另一方面,当我们谈论javascript时。 We know there could not be any overloading in it. 我们知道它不会有任何超载。 They made a single function and have optional parameter. 他们只做了一个功能,并有可选参数。 If you provide limit argument it will limit on the number of splits to be found. 如果您提供limit参数,它将限制要找到的拆分数。

Integer specifying a limit on the number of splits to be found. 整数,指定要查找的拆分数限制。 The split() method still splits on every match of separator, until the number of split items match the limit or the string falls short of separator. split()方法仍会在分隔符的每个匹配上拆分,直到拆分项的数量与限制匹配或字符串不足于分隔符。

javascript docs from developer.mozilla.org 来自developer.mozilla.org的javascript文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM