微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐