views:

5057

answers:

5

I am looking for a method to combine an array of strings into a delimited String. An opposite to split(). I've seen this in other languages.

Wanted to ask the forum before I try writing my own( since the JDK has everything...)

Thanks,

+12  A: 

There's no method in the JDK for this that I'm aware of. Apache Commons Lang has various overloaded join methods that do what you want.

John Topley
I just looked in on the apache commons classes - awesome!Probably everything I need..thanks
javaphild
+3  A: 

There are several examples on DZone Snippets if you want to roll your own that works with a Collection. For example:

public static String join(AbstractCollection<String> s, String delimiter) {
    if (s == null || s.isEmpty()) return "";
    Iterator<String> iter = s.iterator();
    StringBuilder builder = new StringBuilder(iter.next());
    while( iter.hasNext() )
    {
        builder.append(delimiter).append(iter.next());
    }
    return builder.toString();
}
Bill the Lizard
I would be inclined to leave out the empty check, because the behavior will be the same and why complicate the code with special cases? On the other hand, a null check might be in order.
Carl Manaster
@Carl: I completely agree with you on the null check and added that. Checking for an empty Collection allows the function to skip needlessly creating an Iterator and a StringBuffer, so I left it in.
Bill the Lizard
You should use StringBuilder instead of StringBuffer as StringBuffer has the added overhead of being synchronized. You don't need synchronization for method variables (since only one thread will be using it).
Steve Kuo
@Steve: Agreed and updated.
Bill the Lizard
Your join method should accept Collection, not AbstractCollection. Better yet, it could accept Iterable, which allows it to be more generic.
Steve Kuo
@Steve: Either of those would be acceptable. You can make it as abstract or specific as you need. There's a later example on the page I linked to that accepts Iterable.
Bill the Lizard
+4  A: 

I got the following example here

/*
7) Join Strings using separator >>>AB$#$CD$#$EF

 */

import org.apache.commons.lang.StringUtils;

public class StringUtilsTrial {
  public static void main(String[] args) {

    // Join all Strings in the Array into a Single String, separated by $#$
    System.out.println("7) Join Strings using separator >>>"
        + StringUtils.join(new String[] { "AB", "CD", "EF" }, "$#$"));
  }
}
Azder
+1  A: 

Google also provides a joiner class in their Google Collections library:

Joiner API

Google Collections

Blair Zajac
A: 

Based on all the previous answers:

public static String join(Iterable<? extends Object> elements, CharSequence separator) 
{
    StringBuilder builder = new StringBuilder();

    if (elements != null)
    {
        Iterator<? extends Object> iter = elements.iterator();
        if(iter.hasNext())
        {
            builder.append( String.valueOf( iter.next() ) );
            while(iter.hasNext())
            {
                builder
                    .append( separator )
                    .append( String.valueOf( iter.next() ) );
            }
        }
    }

    return builder.toString();
}
Sarabjot