Java split – wrong length

Why do I receive a length of 3 instead of 4? How can I solve this problem to get the right length?

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

Solution

You get a length of 3 because split,

If you specify a negative limit, it will work properly:

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

You just need to ignore or delete item 5, or delete the trailing; – It is displayed because there are 5 (possibly blank) strings on both sides of the 4 tokens For more information, see docs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>