字符串split()方法,零极限和负极限 [英] String split() method, zero and negative limit

查看:61
本文介绍了字符串split()方法,零极限和负极限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.tutorialspoint.com/java/java_string_split.htm

这里是此方法的语法: public String [] split(String regex,int limit) public String [] split(String regex)

Here is the syntax of this method: public String[] split(String regex, int limit) or public String[] split(String regex)

在上面的链接中,我可以理解 Str.split(-",2) Str.split(-",3)示例.

In the above link, I can understand the Str.split("-", 2) and Str.split("-", 3) examples.

但是,我不太了解 Str.split(-",0),零为零的作用是什么?在某些示例中,我还遇到了负限制,这是什么?

However, I don't quite 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?

推荐答案

limit 参数控制模式的应用次数,因此会影响生成的 array .对于此限制,我们有 3 个可能的:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. We have 3 possible values for this limit:

  1. 如果限制 n 大于,则该模式将最多应用 n-1 次, array的长度将不大于 n ,并且 array的的最后一个条目将包含除最后匹配的定界符之外的所有输入.

  1. 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 是非正数,则该模式将被尽可能多地应用,并且 array 可以具有任意长度.

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

如果 n 为零,则该模式将被尽可能多地应用, array 可以具有任意长度,并且尾随的空字符串将被丢弃.

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.

您可以阅读更多此处.

这篇关于字符串split()方法,零极限和负极限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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