Convert string array to string with separator I have this little convenience method that takes an String array and returns an String where the values are separated by a provided separator (comma, pipe, etc). 

For instance, if given an array with values "abc", "def" and "ghi", and a for separator we passed " | ", the end result would be "abc | def | ghi".

This is the method:

public String getArrayAsString(String[] stringArray, String separator) {
    StringBuilder finalString = new StringBuilder();
    boolean needsSeparator = false;
    for (String s : stringArray) {
        if (needsSeparator) {
            finalString.append(separator);
        }
        finalString.append(s);
        needsSeparator = true;
    }
    return finalString.toString();
}


Notice that the separator must, as its name implies, separate values, and not be appended or prepended artificially to the final String. So, for instance, the following would be incorrect return values: "abc | def | ghi | " or " | abc | def | ghi". 

I specifically don't like the needsSeparator flag variable, but without it, the unwanted leading or trailing separators would be added, and then I would need to remove them from the finalString before returning it, which I also don't find to be very elegant. 

Is there a more concise (perhaps more elegant) way to achieve this?
Before I review your code I want to point out that Java has a String.join() method which does exactly that. Now to your code:

You use StringBuilder, good. You take care of not appending a seperator after the last and not prepending before the first element, also good. You don't handle null values though, it's your decision if you want to handle them separately or just let Java just throw a NPE. For the separator however, a null appends literally null to the String, I'd rather default to "" if separator is null. A way to get rid of the flag:

public String getArrayAsString(String[] stringArray, String separator) {
    if(stringArray == null || stringArray.length == 0) return "";


With this we already ensured that our stringArray has at least one entry. What we can do now is initialize the StringBuilder with the first String:

    StringBuilder finalString = new StringBuilder(stringArray[0]);


Since all strings after the first one need to prepend the separator, we don't need the flag anymore. Unfortunately, we can't use a foreach loop, since we have to start at index 1 as the element at index 0 is already in the StringBuilder:

    for(int i = 1; i &lt; stringArray.length; ++i)
        finalString.append(separator).append(stringArray[i]);


And then just return what we got:

    return finalString.toString();


Whole function:

public String getArrayAsString(String[] stringArray, String separator) {
    if(stringArray == null || stringArray.length == 0) return "";

    StringBuilder finalString = new StringBuilder(stringArray[0]);

    for(int i = 1; i &lt; stringArray.length; ++i)
        finalString.append(separator).append(stringArray[i]);

    return finalString.toString();
}


Edit:

In String.join() the parameters are swapped, so the separator comes first, the array second:

    String[] arr = new String[]{"Hello", "World"};
    System.out.println(String.join(" | ", arr));
    System.out.println(getArrayAsString(arr, " | ")); //Your function


Output:

Hello | World
Hello | World


Working code at ideone.com