Let's explore more see the interesting results of split below:
System.out.println(",,,,,,".split(",").length); // 0
System.out.println(",,,,,, ".split(",").length); // 7
System.out.println(",,, ,,,".split(",").length); // 4
System.out.println(" ,,,,,,".split(",").length); // 1
Wondering if why it's happening this is because below statement stated for the split method in docs:
Trailing empty strings are therefore not included in the resulting
array.
Docs: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
if you don't want the split method to remove that spaces then you should use another split with limit:
public String[] split(String regex,int limit)
Docs: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int)
Example:
System.out.println(",,,,,,".split(",",-1).length); // 7
System.out.println(",,,,,, ".split(",",-1).length); // 7
System.out.println(",,, ,,,".split(",",-1).length); // 7
System.out.println(" ,,,,,,".split(",",-1).length); // 7