+ Start a Discussion
AlexPHPAlexPHP 

Is there a List/Set join() / implode() method?

I'm looking for a method that will have a join/implode functionality.

 

I cannot seem to find an equivalent in the docs.  Does one exist in APEX?

 

Thanks

sfdcfoxsfdcfox

Not in the PHP/Java/Perl/Ruby/JavaScript/insert-favorite-scripting-language-here sense of the meaning. You'd have to do this:

 

 

String join(List<String> values) {
  List<String> valueCopy = new List<String>(values);
  if(valueCopy.isEmpty())
    return null;
  String result = valueCopy[0];
  valueCopy.remove(0);
  while(!valueCopy.isEmpty()) {
    result += ',' + valueCopy[0];
    valueCopy.remove(0);
  }
  return result;
}

You can use the same with Sets as well.

 

MRosiMRosi

This is an answer to an old post, but it is available not in Apex but in the apex-lang utility code library from Google inspired by the Apache Commons Lang project. The above reply also works I guess but I noticed this lib also gives a lot of other useful functions and even has a csv parser. So..

 

http://code.google.com/p/apex-lang/

 

I found it myself now and am just installing it, but there are several methods filling the description in there, inside StringUtils.

 

http://code.google.com/p/apex-lang/source/browse/trunk/eclipse-project/src/classes/StringUtils.cls

http://richardvanhook.com/2009/05/05/getting-started-with-apex-lang/

 

Here's a sample:

 

global static String joinArray(Object[] objectArray, String separator) {

        if (objectArray == null) {

            return null;

        }

        return joinArray(objectArray, separator, 0, objectArray.size());

    }

 



Reinier van den Assum | DeloitteReinier van den Assum | Deloitte
At the moment you will be able to join an iterable object (such as List) using 
List<String> listVariable = new List<String>{'value1', 'value2'};
String separator = ', ';
String.join(listVariable, separator);

This will return
value1, value2
Most likely, this is added in a later API, but I thought it would be good to add this information to this open discussion.
Nathan HincheyNathan Hinchey
@Reiner van den Assum's answer should be marked as correct.
@Amit Kumar Giri@Amit Kumar Giri
++ @reinier van -  iterable object join works perfect today also vs other Apex API logic to join. iterable seems best to me as today.
Lincoln KaruhangaLincoln Karuhanga
What was the point of making the join API different from the Java one?
Adam Cadman 4Adam Cadman 4
@MRosi
The apex-lang plugin looks like it could be really useful but...
The third link to the richardvanhook website insists that you install a Chrome browser security plugin before you can access the content.
Legit sites don't do this!

If you visit the root of the website, there are loads of browser warnings - it looks like the site has been well and truly compromised and is now full of malware. Be careful - this site is also linked to from the Wiki section of the app's Project website.

Is apex-lang safe to install anymore? I don't know, but I'm not going to risk it! It also has not been updated since 2012, which is a pity - much of the functionality I think is still missing from Apex.
So much for the promise, on the project home page, of many more updates per year than SF's 3 or 4!