[SalesForce] String split function not working properly. Any suggestions please

I tried to split a string using the delimiter [.] and the list size after split is 3 which is wrong and it should be 9 which is wrong. How to get the correct list size? Why this happening? Any suggestions please.

String va='300430[.][.]testcompany crm[.][.][.][.][.][.]';
List<String> li = va.split('\\[.]');
System.debug('li size >>>>>>>>>>>'+li.size());

Best Answer

After some Google search, i have found an example in Java, fortunately Salesforce accepted the same syntax that is for Java and gave the result as i expected.

The following code do the trick. The split method given in the Salesforce documentation does not provide this variation.

Specifying -1 as the second parameter in the split method considers the "empty" while splitting a string to a list or an array.

String va='300430[.][.]testcompany crm[.][.][.][.][.][.]';
List<String> li = va.split('\\[.]',-1);
System.debug('li size >>>>>>>>>>>'+li.size());