views:

2961

answers:

6

I want to join a String[] with a glue string. Is there a function for this?

+17  A: 

Apache Commons Lang has a StringUtils class which has a join function which will join arrays together to make a String.

For example:

StringUtils.join(new String[] {"Hello", "World", "!"}, ", ")

Generates the following String:

Hello, World, !
coobird
A: 

Not in core, no. A search for "java array join string glue" will give you some code snippets on how to achieve this though.

e.g.

public static String join(Collection s, String delimiter) {
    StringBuffer buffer = new StringBuffer();
    Iterator iter = s.iterator();
    while (iter.hasNext()) {
        buffer.append(iter.next());
        if (iter.hasNext()) {
            buffer.append(delimiter);
        }
    }
    return buffer.toString();
}
Brad Beattie
Use StringBuilder (non-thread-safe) in place of StringBuffer (thread-safe) for better performance. The interface is the same.
Asaph
There's many ways the performance of that could be improved.
Tom Hawtin - tackline
This seems to be from http://snippets.dzone.com/posts/show/91. The comments suggest a much improved version:public static String join( Iterable< ? extends Object > pColl, String separator ) { Iterator< ? extends Object > oIter; if ( pColl == null || ( !( oIter = pColl.iterator() ).hasNext() ) ) return ""; StringBuilder oBuilder = new StringBuilder( String.valueOf( oIter.next() ) ); while ( oIter.hasNext() ) oBuilder.append( separator ).append( oIter.next() ); return oBuilder.toString(); }
Quantum7
+2  A: 

Nothing built-in that I know of.

Apache Commons Lang has a class called StringUtils which contains many join functions.

JonMR
+3  A: 

But then, you could easily write such a function with about ten lines of code. Like:

String combine(String[] s, String glue)
{
  int k=s.length;
  if (k==0)
    return null;
  StringBuilder out=new StringBuilder();
  out.append(s[0]);
  for (int x=1;x<k;++x)
    out.append(glue).append(s[x]);
  return out.toString();
}

Edit: A better signature might be combine(String glue, String... s), so you could pass it either an array or a comma-separated list of strings. Whatever.

Jay
It's good practice to include curly braces even on one liner ifs and fors.
Asaph
Good practice according to you...there is no universal standard for such things.
phoebus
Fortunately for Java there are. http://java.sun.com/docs/codeconv/
Bombe
Yes, always use the curly braces. It's what makes Java so nice and cosy! Even though SUN does not always add them in their own code samples.
Adriaan Koster
But if I included braces, that would have added two more lines of code, and I couldn't have claimed to have done it in 11 lines!
Jay
A: 

java.util.Arrays has an 'asList' method. Together with the java.util.List/ArrayList API this gives you all you need:;

private static String[] join(String[] array1, String[] array2) {

    List<String> list = new ArrayList<String>(Arrays.asList(array1));
    list.addAll(Arrays.asList(array2));
    return list.toArray(new String[0]);
}
Adriaan Koster
The question is how to join an array of string with a delimiter, not how to join two arrays of strings together.
toolbear74
A: 

I need to compose a variable length array of strings 10-2000. Is there a better method?

Dr.Vet. Cumpanasu Florin
You should post follow-up questions as a separate thread, notas an answer. After all, it doesn't really answer *this*question. Also more people would see it and try to answer if you post it asyour own question.
sth