You need to sign in to do that
Don't have an account?

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
Thanks
Sudhir
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
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
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
Thanks
DG