function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MaheemSamMaheemSam 

List to comma, There is a comma at the end how to remove

Hi,
  
 In below code it converts list to a comma seprated value problem here it added comma at the end please suggest me how to remove the end comma 
Boolean chkcomma = false;

// a list of string, you wanna display

list<String> QtAprd = new list<string>{'A','B','C','D','E','F'};

// making a single string with comma seprated from above list

String commaSepratedList='';

for(String str : QtAprd)

{
 if (chkcomma)
     
 commaSepratedList += str + ',' ;
  chkcomma = true;
}

system.debug(commaSepratedList);

Thanks
Sudhir
Best Answer chosen by MaheemSam
Maharajan CMaharajan C
Hi Sudhir,

Please use the below ways:

1. )  Use the removeEnd method :

Boolean chkcomma = false;

// a list of string, you wanna display

list<String> QtAprd = new list<string>{'A','B','C','D','E','F'};

// making a single string with comma seprated from above list

String commaSepratedList='';

for(String str : QtAprd)

{
 if (chkcomma)
     
 commaSepratedList += str + ',' ;
  chkcomma = true;
}

String s2 = commaSepratedList.removeEnd(',');
system.debug('@@@ '+s2);


==============

2. Or Use the join method:

Boolean chkcomma = false;

// a list of string, you wanna display

list<String> QtAprd = new list<string>{'A','B','C','D','E','F'};

list<String> strList = new List<String>();

// making a single string with comma seprated from above list

String commaSepratedList='';

for(String str : QtAprd)

{
 if (chkcomma)
     
    strList.add(str);

  chkcomma = true;
}

commaSepratedList = String.join(strList,',');

system.debug('@@@ '+commaSepratedList);

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Sudhir,

Please use the below ways:

1. )  Use the removeEnd method :

Boolean chkcomma = false;

// a list of string, you wanna display

list<String> QtAprd = new list<string>{'A','B','C','D','E','F'};

// making a single string with comma seprated from above list

String commaSepratedList='';

for(String str : QtAprd)

{
 if (chkcomma)
     
 commaSepratedList += str + ',' ;
  chkcomma = true;
}

String s2 = commaSepratedList.removeEnd(',');
system.debug('@@@ '+s2);


==============

2. Or Use the join method:

Boolean chkcomma = false;

// a list of string, you wanna display

list<String> QtAprd = new list<string>{'A','B','C','D','E','F'};

list<String> strList = new List<String>();

// making a single string with comma seprated from above list

String commaSepratedList='';

for(String str : QtAprd)

{
 if (chkcomma)
     
    strList.add(str);

  chkcomma = true;
}

commaSepratedList = String.join(strList,',');

system.debug('@@@ '+commaSepratedList);

Thanks,
Maharajan.C
This was selected as the best answer
Deepak GulianDeepak Gulian
Just simply use this code
List<String> QtAprd = new List<string>{'A','B','C','D','E','F'};
String commaSeparatedString = String.join(QtAprd, ',');
System.debug(commaSeparatedString);

Thanks
DG