What's the best way to build a string of delimited items in Java?

What's the best way to build a string of delimited items in Java?

what meaning
what definition
what w
what does
what is called
how to pronounce what
what h

While working in a Java app, I recently needed to assemble a comma-delimited list of values to pass to another web service without knowing how many elements there would be in advance. The best I could come up with off the top of my head was something like this:

public String appendWithDelimiter( String original, String addition, String delimiter ) {
    if ( original.equals( "" ) ) {
        return addition;
    } else {
        return original + delimiter + addition;

String parameterString = "";
if ( condition ) parameterString = appendWithDelimiter( parameterString, "elementName", "," );
if ( anotherCondition ) parameterString = appendWithDelimiter( parameterString, "anotherElementName", "," );

I realize this isn't particularly efficient, since there are strings being created all over the place, but I was going for clarity more than optimization.

In Ruby, I can do something like this instead, which feels much more elegant:

parameterArray = [];
parameterArray << "elementName" if condition;
parameterArray << "anotherElementName" if anotherCondition;
parameterString = parameterArray.join(",");

But since Java lacks a join command, I couldn't figure out anything equivalent.

So, what's the best way to do this in Java?

using Dollar is simple as typing:

String joined = $(aCollection).join(",");

NB: it works also for Array and other data types


Internally it uses a very neat trick:

public String join(String separator) {
    Separator sep = new Separator(separator);
    StringBuilder sb = new StringBuilder();

    for (T item : iterable) {

    return sb.toString();

the class Separator return the empty String only the first time that it is invoked, then it returns the separator:

class Separator {

    private final String separator;
    private boolean wasCalled;

    public Separator(String separator) {
        this.separator = separator;
        this.wasCalled = false;

    public String toString() {
        if (!wasCalled) {
            wasCalled = true;
            return "";
        } else {
            return separator;

What, P!nk's official music video for 'So What'. Click to listen to P!nk on Spotify: http:// smarturl.it Duration: 3:46 Posted: Oct 25, 2009 WhatsApp works with Safari 9+ To use WhatsApp, update Safari or use Google Chrome, Mozilla Firefox or Opera.. Update Safari

Slight improvement [speed] of version from izb:

public static String join(String[] strings, char del)
    StringBuilder sb = new StringBuilder();
    int len = strings.length;

    if(len > 1) 
       len -= 1;
       return strings[0];

    for (int i = 0; i < len; i++)


    return sb.toString();

P!nk - So What (Official Music Video), I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)� noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.

Fix answer Rob Dickerson.

It's easier to use:

public static String join(String delimiter, String... values)
    StringBuilder stringBuilder = new StringBuilder();

    for (String value : values)

    String result = stringBuilder.toString();

    return result.isEmpty() ? result : result.substring(0, result.length() - 1);

what, what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more. what definition: 1. used to ask for information about people or things: 2. used in questions that show you are…. Learn more.

I personally quite often use the following simple solution for logging purposes:

List lst = Arrays.asList("ab", "bc", "cd");
String str = lst.toString().replaceAll("[\\[\\]]", "");

WHAT, Quickly send and receive WhatsApp messages right from your computer. whatever: adverb at all , of any description , of any kind or sort , whatsoever , whichever

If you want to apply comma in a List of object's properties. This is the way i found most useful.

here getName() is a string property of a class i have been trying to add "," to.

String message = listName.stream().map(list -> list.getName()).collect(Collectors.joining(", "));

WhatsApp Web, It is plural if a plural noun or noun phrase completes the sentence, as in He sometimes makes what seem to be gestures of reconciliation. � Clauses with what as� The ORIGINAL and OFFICIAL "What What (In the Butt)" video! Samwell will CALL your friends and family! Go here: http://officialsamwell.com/live-call/ This is

What, U.S. law requires employers to create a workplace free from discrimination and harassment. But as offices go virtual, what happens when staff� WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

What to Do When Your Employee Is Harassed Online, Before you work yourself into a frenzy, take a moment to assess what's really going on and identify what's causing the strain in your relationship� Seen a font in use and want to know what it is? Upload an image to WhatTheFont to find the closest matches in our database.

What to Do If Your Boss Doesn't Like You, Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.