Java traps之字符串split

来源:互联网 发布:表白 网页 源代码 php 编辑:程序博客网 时间:2024/06/16 09:38

字符串split

Java中,字符串的split(“,”)与split(“,”, -1)的区别:一个忽略末尾的空字符,一个不忽略末尾的空字符。
例如:

"test,,"split(",")返回的数组长度为1split(",", -1)返回的数组长度为3

jdk中关于split(String regex, int limit)中参数limit的说明:

 * <p> The {@code limit} parameter controls the number of times the * pattern is applied and therefore affects the length of the resulting * array.  If the limit <i>n</i> is greater than zero then the pattern * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's * length will be no greater than <i>n</i>, and the array's last entry * will contain all input beyond the last matched delimiter.  If <i>n</i> * is non-positive then the pattern will be applied as many times as * possible and the array can have any length.  If <i>n</i> 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.
原创粉丝点击