加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java拆分 – 错误的长度

发布时间:2020-12-15 04:26:06 所属栏目:Java 来源:网络整理
导读:为什么我收到长度为3而不是4?我怎样才能解决这个问题以获得合适的长度? String s="+9851452;;FERRARI;;";String split[]=s.split("[;]");System.out.println(split.length); 解决方法 你得到的长度为3,因为 split , This method works as if by invoking t
为什么我收到长度为3而不是4?我怎样才能解决这个问题以获得合适的长度?

String s="+9851452;;FERRARI;;";
String split[]=s.split("[;]");
System.out.println(split.length);

解决方法

你得到的长度为3,因为 split,

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

如果您指定负限制,它将正常工作:

String s="+9851452;;FERRARI;;";
    String split[]=s.split(";",-1);
    System.out.println(Arrays.toString(split));

你只需要忽略或删除第5项,或删除尾随; – 它显示是因为4个令牌的两边有5个(可能是空白的)字符串.有关详细信息,请参阅docs.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读