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());

2 Answers 2


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());
  • if it work as expected this looks very good.
    – RichardT
    Dec 2, 2014 at 13:22

your example

String va='300430[.][.]testcompany crm[.][.][.][.][.][.]';

splits into

[ "300430", "", "testcompany crm" ]

what happens is, that if salesforce hits trailing "pure" delimiters, they (somehow) got lost. I mean if you have just delimiters without anything between them at all and that in the end, then the will be ignored. No idea why this happens - could be a bug.

other example

String va='[.][.][.][.]300430[.][.][.]2[.]testcompany crm[.]56[.]45[.][.][.][.]45[.]55[.][.]';

splits into [ "", "", "", "", "300430", "", "", "2", "testcompany crm", "56", "45", "", "", "", "45", "55" ]

you see: in the front it works and in the end, it does not.

to make it better, you could try to add a constant suffix and delete the last element of the list

String va='[.][.][.][.]123[.][.][.]2[.]test[.][.][.]' + '[.]foobar';

note that i added [.]foobar

you get

[ "", "", "", "", "123", "", "", "2", "test", "", "", "", "foobar" ]

if you now

li.remove( li.size()-1 );

you will get what you really want i think

[ "", "", "", "", "123", "", "", "2", "test", "", "", "" ]
  • Thanks RichatT for the great explanation. After some search i found an alternative which could be useful as well. Please see my above answer and let me know your thoughts.
    – Bforce
    Nov 30, 2014 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .