Java traps之字符串split

本文详细解释了Java中字符串split方法的使用方式,特别是split(“,”)与split(“,”,-1)的区别:前者会忽略末尾的空字符,后者则保留。通过具体示例展示了如何应用这些方法来解析字符串,并提供了关于split(String regex,int limit)中参数limit的官方说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串split

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

"test,,"
split(",")返回的数组长度为1
split(",", -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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值