Using multiple consecutive commas to separate strings in Java

See English answers > how to split a comma delimited string into an array of empty strings1

String abc = "a,b,c,d,";
String[] arr = abc.split(",");
System.out.println(arr.length);

The output is 4 But obviously my expectation is 7 This is my solution:

String abc = "a,";
abc += "\n";
String[] arr = abc.split(",");
System.out.println(arr.length);

Why is that? Who can give me a better solution?

Solution

This is done using the alternative version of the string #split () with two parameters:

String abc = "a,",-1);
System.out.println(arr.length);

This print

7

Javadoc linked from above:

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
分享
二维码
< <上一篇
下一篇>>