为什么返回的数组对于Java String.split()具有不同的长度?

时间:2018-12-04 08:28:50

标签: java

String.split()在Java中到底做什么?为什么情况2和情况3的长度不同?

案例1:

String line = "a,b,c,d";
String[] parts = line.split(",");
System.out.println("Length:" + parts.length);

Length: 4

案例2:

String line = "a,,,d";
String[] parts = line.split(",");
System.out.println(parts.length);

Length: 4

案例3:

String line = "a,b,,";
String[] parts = line.split(",");
System.out.println(parts.length);

Length: 2

0 个答案:

没有答案