为什么我收到的长度是 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了解更多信息。
关于Java Split - 长度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29040530/