Java - String split() 方法,零和负限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24701197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - String split() Method, zero and negative limit
提问by
http://www.tutorialspoint.com/java/java_string_split.htm
http://www.tutorialspoint.com/java/java_string_split.htm
Here is the syntax of this method: public String[] split(String regex, int limit)
or public String[] split(String regex)
这是此方法的语法:public String[] split(String regex, int limit)
或public String[] split(String regex)
In the above link, I can understand the Str.split("-", 2)
and Str.split("-", 3)
examples. However, I don't quit get the Str.split("-", 0)
, what's the role of zero of the limit? In some examples, I have also encountered negative limits, what is this? Thanks a lot
在上面的链接中,我可以理解Str.split("-", 2)
和Str.split("-", 3)
示例。但是,我不退出获取Str.split("-", 0)
,限制零的作用是什么?在一些例子中,我也遇到过负限制,这是什么?非常感谢
采纳答案by GingerHead
The limitparameter controls the number of times the patternis applied and therefore affects the length of the resulting array
. We have 3possible valuesfor this limit:
的极限参数控制的次数图案被施加,因此影响所得的长度array
。对于此限制,我们有3 个可能的值:
If the limit nis greater than zerothen the pattern will be applied at most n - 1times, the
array's
length will be no greater than n, and thearray's
last entry will contain all input beyond the last matched delimiter.If nis non-positive then the pattern will be applied as many times as possible and the
array
can have any length.If nis 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大于零,则该模式将最多应用n - 1次,
array's
长度将不大于n,并且array's
最后一个条目将包含最后一个匹配的分隔符之外的所有输入。如果n为非正数,则该模式将被应用尽可能多的次数并且
array
可以具有任意长度。如果n为零,则该模式将被应用尽可能多的次数,
array
可以具有任意长度,并且将丢弃尾随的空字符串。
You can read more here.
您可以在此处阅读更多内容。
回答by Eran
Str.split("-", 0)
is equivalent to Str.split("-")
. I.e, there's no limit.
Str.split("-", 0)
相当于Str.split("-")
。即,没有限制。
回答by dari
Str.split("-",0)
is the same as Str.split("-")
Str.split("-",0)
是相同的 Str.split("-")