I have five string which contains some value I need to check if any of them contains value if yes then store them in Another string.

Currently I'm doing this like below:

List<string> slist = new List<string>();
String s1 = 'abc';
string s2 = 'bcd';
string s3 = 'cde';
string s4 = 'def';
string s5 = 'efg';

string allstirng;
boolean check;
for(String s: slist){
        allstirng +=',';
    allstirng += s;
    check = false;

Is there any other better way of doing this.

  • Aren't you missing the logic for your 'check' boolean? something like: String.isNotBlank(s). Besides, according to your question, you add your evaluated string to another one only if the first contains a value, but your code stores all the strings regardless of the value they have.
    – Joca
    Jul 17, 2015 at 19:41

3 Answers 3


As of at least 2022 this no longer works as the .remove(null) was undocumented and its use seems to have been fixed

I am a little confused as there are no nulls but, not to mention just create the all string directly (unless you are trying to understand the concept :

List<string> slist = new string[]{'abc','bcd','cde','def','efg'};

string allstring = string.join(sList,',');

Is a simplified version of your code

  • 2
    You type faster than me. I was going to post (almost) the same code :D.
    – Joca
    Jul 17, 2015 at 19:33
  • 1
    This code throws a fatal error when run in Execute Anonymous: System.NullPointerException: Attempt to de-reference a null object. The remove method on the List class expects an index integer, not a null. Not sure why this has been upvoted so many times. Mar 29, 2022 at 20:57
  • 1
    @MatthewSouther - You are correct this no longer works. It did work at the time of posting however. Will update to reflect.
    – Eric
    Apr 25, 2022 at 23:03

The top upvoted answer here throws a fatal error, as I commented below it. Don't use it.

If you don't expect your list to have any duplicates, you can use the remove method of the Set class to get rid of specific values:

List<String> slist = new List<String>{'abc', 'bcd', 'cde', null, 'def', 'efg'};
Set<String> sset = new Set<String>(slist);
String allstring = String.join(new List<String>(sset), ',');
System.debug(allstring); // abc,bcd,cde,def,efg

Otherwise, iterate backwards through your list to remove nulls (or other specific values):

List<String> slist = new List<String>{ 'abc', null, 'bcd', 'abc', null, 'def' };
for (Integer i = slist.size() - 1; i >= 0; i--) {
    if (slist[i] == null) {
String allstring = String.join(slist, ',');
System.debug(allstring); // abc,bcd,abc,def

Especially for those people who constantly use this solution, please use ready-made methods. This will speed up your work with the code.

public List<String> StrToList(String str) {
    List<String> listNew = new List<String>();
    listNew = str.split(',');
    return listNew;

public String ListToString(List<String> aList) {
    String allstirng='';
    Boolean check = false;
    for (String s : aList) {
        if (check) allstirng += ',';            
        allstirng += s;
        check = true;
    return allstirng;

public String ListToString(List<String> aList) {
    String allstring = string.join(aList,',');
    return allstring;
  • 8
    Hello, welcome to SFSE. This is what we often call a "code dump" answer - it is of limited value because it does not provide any explanation to the asker or the community. It's also not clear how this answers the question, which has been marked as "solved" for more than five years.
    – David Reed
    Jan 4, 2019 at 12:20

You must log in to answer this question.

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