0

Apex String split() results an empty list item even there is no value.

For example,

String test = '';
list<string> test1 = test.split(',');
System.debug(test1.size());    // Result = 1

so I had my own method to omit empty list items. Is there a better function to do splitting and to omit empty ones.

Appreciate any help on this. Thanks.

3

Your value is an empty string. There is nothing to split that matches your criteria which means the whole string (empty string) will be the result and it will be placed at the first index in the result array... '' is still a value at the end of the day

0

Hi You can add if condition before spliting

String test = '';

if(string.isNotEmpty(test)){
      list<string> test1 = test.split(',');
         System.debug(test1.size());   
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.