If my list of string is in format like 'United kindom,US,EUROPE,Canda';

String result = '';
for(string str: countries){
    result = result + ',' + str; 

string output = result.removeStart(',');

System.debug('Output =' + output);

I am expecting output like Output = 'America','Australia','Canada','United Kingdom'

  • 2
    Aside from your String[] country line having some syntax issues, what are you stuck on here? I can think of a better way to do what you want to do, but the code you have should produce your desired output. – Derek F Aug 22 '19 at 20:33
  • 1
    Yeah, this definitely seems like an XY Problem. What is your end goal here? – Adrian Larson Aug 22 '19 at 20:49
  • I have added first line code for showing the list of string, actually that list of string is retrieving from other class I am looking for an output will be - 'America', 'Australia', 'Canada', 'United Kingdom' – Stephen Roy Aug 22 '19 at 20:57
  • 1
    And how is that different from what you're currently seeing? Along the same lines as Adrian's question for you, why do you need the data in that specific format? You're not going to get accurate answers unless you are clear and detailed. – Derek F Aug 22 '19 at 22:13

If I understand correctly, you're looking for something like:

String [] Country = New List<String>{'America','Australia','Canada','United Kingdom'}; 
system.debug('\'' + String.join(Country,'\',\'') + '\'');
  • I've taken the liberty of formatting the code in your answer for you. For the future, you can reference the help page on formatting. – Derek F Aug 22 '19 at 20:37
  • Thanks, @DerekF. – Chris Johnson Aug 22 '19 at 20:39
  • throwing Error - Method does not exist or incorrect signature: void join(String, String) from the type String. – Stephen Roy Aug 22 '19 at 21:10
  • @ChrisJohnson - are you sure you declared Country an array of strings like Chris did above? – cropredy Aug 23 '19 at 0:36
  • Yes, I can understand but i am looking for output should be like 'US','UK', - My input is list of string retrieving from other class, here i mentioned just for understanding purpose. – Stephen Roy Aug 23 '19 at 8:04

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