A quick and easy way to join array elements with a separator (the opposite of split) in Java [duplicate]
Asked Answered
W

15

592

See Related .NET question

I'm looking for a quick and easy way to do exactly the opposite of split so that it will cause ["a","b","c"] to become "a,b,c"

Iterating through an array requires either adding a condition (if this is not the last element, add the seperator) or using substring to remove the last separator.

I'm sure there is a certified, efficient way to do it (Apache Commons?)

How do you prefer doing it in your projects?

Was answered 30/12, 2009 at 7:38 Comment(3)
As some users have already said, StringUtils.join() will do the job and it's been there forever. No need for Java 8.Armbrecht
Try with new way in kotlin : #40822804Dobrinsky
use java.util.StringJoinerCentaury
E
937

Using Java 8 you can do this in a very clean way:

String.join(delimiter, elements);

This works in three ways:

1) directly specifying the elements

String joined1 = String.join(",", "a", "b", "c");

2) using arrays

String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);

3) using iterables

List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
Elation answered 4/10, 2014 at 17:2 Comment(12)
We should mention that this approach works only for List<CharSequence> or CharSequence[] elements like list or arrays of Strings, StringBuilder.Breeding
@Breeding That's already listed in the comments, it works if you use a varargs argument, arrays or an iterableElation
OK emphasis in my previous comment was used incorrectly. My point is that we can use it only for CharSequence elements like String (as example in question) but it would be good to add info that this method will not work for elements like Person, Car where we need to explicitly call toString.Breeding
It took them 8 major versions to implement something this basic and useful? Wish I could down-vote Java for this.Gottfried
how does it work if one of elements is null or "" ?Just the delimiter will be returned?? It's not so goodCerumen
Unfortunatelly Android Studio does not fully support 1.8 only 1.7: https://mcmap.net/q/41612/-is-jdk-1-8-fully-supported-by-android-studio/239219Countertenor
@Cerumen Take a look hereMassasauga
@AtiRanzuglia after 2 years you referenced me to java 8 documentation?? really? have you been learning java 8 all this time? c'mon!!Cerumen
@Cerumen Haha, didn't realise it was a long time ago ... next time do the search yourself ;)Massasauga
@AtiRanzuglia you are so funny! my question was rhetorical... just to attract authors' attention to specific situations and of course to look at the documentation for these cases :)Cerumen
@MaxHeiber That would be like downvoting an answer because you don't like the previous revision while the current revision doesn't have the same problem. I hope you don't do that either.Incipient
for an array or a list of objects you can use streams (Java 8): Arrays.steam(array).map(String::valueOf).collect(Collectors.joining(delimiter));Hexapod
C
326

If you're on Android you can TextUtils.join(delimiter, tokens)

Chad answered 18/7, 2014 at 23:14 Comment(1)
Still best answer because accepted requires API level 26.Hybrid
P
119

I prefer Guava over Apache StringUtils for this particular problem:

Joiner.on(separator).join(array)

Compared to StringUtils, the Joiner API has a fluent design and is a bit more flexible, e.g. null elements may be skipped or replaced by a placeholder. Also, Joiner has a feature for joining maps with a separator between key and value.

Parol answered 30/12, 2009 at 7:48 Comment(5)
This, however, accepts only Iterable<?>, so simple array has to be retyped.Voguish
@anoniim Joiner.join in what is now Google Guava is overloaded for Iterable as well as Arrays: docs.guava-libraries.googlecode.com/git-history/release/javadoc/…Parol
Ah, you're right. I accidentally used com.google.api.client.util.Joiner instead of com.google.common.base.Joiner which accepts both Iterable and Array.Voguish
Must call skipNulls Joiner.on(separator).skipNulls().join(array)Jackiejackinoffice
Changed the accepted answer (just for future visitors, Java 8 obviously didn't exist when you answered it... sorry for taking it away, it was well deserved!)Was
R
113

Apache Commons Lang does indeed have a StringUtils.join method which will connect String arrays together with a specified separator.

For example:

String[] s = new String[] {"a", "b", "c"};
String joined = StringUtils.join(s, ",");  // "a,b,c"

However, I suspect that, as you mention, there must be some kind of conditional or substring processing in the actual implementation of the above mentioned method.

If I were to perform the String joining and didn't have any other reasons to use Commons Lang, I would probably roll my own to reduce the number of dependencies to external libraries.

Regan answered 30/12, 2009 at 7:42 Comment(4)
in android I wasn't able do this, I had to change some String fixedHours = StringUtil.join(Arrays.asList(hoursSplit), " | ");Irrigation
I tried to use this method with " " as separator (also tried ' '), but that separator is ignored. I don't have time to check if they do a trim or whatever on it but in any case it's not what I needed ;-)Cleavers
is this method still better than joining with StringBuilder if the array is large e.g. 100k string items of length 80-150 chars considering how StringUtils work and that the String is immutable?Waterhouse
The problem with this method is the delimiter is last. You can't send in a bunch of strings and then the delimiter; it thinks the delimiter is one of the list.Commute
S
54

A fast and simple solution without any 3rd party includes.

public static String strJoin(String[] aArr, String sSep) {
    StringBuilder sbStr = new StringBuilder();
    for (int i = 0, il = aArr.length; i < il; i++) {
        if (i > 0)
            sbStr.append(sSep);
        sbStr.append(aArr[i]);
    }
    return sbStr.toString();
}
Syrup answered 8/3, 2013 at 11:44 Comment(3)
This is advertised as fast though it wastes a lot of time. It checks whether i > 0 every time when we know it will only be true the first time. It is easy to avoid this kind of wasted time. Second, it might be faster to test the size of the incoming strings and pass the result into the constructor of the StringBuilder... haven't test that but I suspect it might be a win... if you really care about "fast".Blob
@Blob actually, if you're considering fast, you can rely on that i > 0 being optimized out thanks to CPU lookahead. Consider https://mcmap.net/q/14898/-why-is-processing-a-sorted-array-faster-than-processing-an-unsorted-array/330057Nipper
The 'i > 0' check is also used in the apache commons implementation. The check is false only in the first iteration.Scutcheon
L
35

With Java 1.8 there is a new StringJoiner class - so no need for Guava or Apache Commons:

String str = new StringJoiner(",").add("a").add("b").add("c").toString();

Or using a collection directly with the new stream api:

String str = Arrays.asList("a", "b", "c").stream().collect(Collectors.joining(","));
Last answered 11/3, 2014 at 7:27 Comment(5)
I do not see the stream method anywhere. Are you sure you are not using some other library?Cuspidation
stream() is available on the Collection interface in Java 1.8: docs.oracle.com/javase/8/docs/api/java/util/…Last
Arrays.asList().stream() can be replaced with Arrays.stream()Legra
myArray.stream().map(a -> a.getValue()).collect(Collectors.joining(", "));Monda
For joining together collections of non strings I find it very convenient to write list.stream().map(String::valueOf).collect(Collectors.joining("|"));Ivette
E
32

"I'm sure there is a certified, efficient way to do it (Apache Commons?)"

yes, apparenty it's

StringUtils.join(array, separator)

http://www.java2s.com/Code/JavaAPI/org.apache.commons.lang/StringUtilsjoinObjectarrayStringseparator.htm

Exorable answered 30/12, 2009 at 7:41 Comment(4)
is this method still better than joining with StringBuilder if the array is large e.g. 100k string items of length 80-150 chars considering how StringUtils work and that the String is immutable?Waterhouse
@zavael, your remark is so highly specific that I have a feeling you really want to tell me that it's not. You could of course research the issue, gather data and then post your own answer based on your research so that others may learn from it, but that would of course be much more work than posting a comment like you just did.Exorable
oh, sorry, I didnt mean to offend you. I just missed some hint if your sollution has general scope, or if it behaves differently with larger base of data, thats all. I think its better to improve existing answers to provide usefull info than to add 16th answer :)Waterhouse
This is universal so that the elements doesn't have to be Strings like in case of Java 8 String.join().Overstuffed
M
22

Even easier you can just use Arrays, so you will get a String with the values of the array separated by a ","

String concat = Arrays.toString(myArray);

so you will end up with this: concat = "[a,b,c]"

Update

You can then get rid of the brackets using a sub-string as suggested by Jeff

concat = concat.substring(1, concat.length() -1);

so you end up with concat = "a,b,c"

if you want to use Kotlin:

 val concat = myArray.joinToString(separator = ",") //"a,b,c"
Madalinemadalyn answered 9/7, 2014 at 13:18 Comment(5)
This method does return a comma-delimited String, but it prints array brackets too,(i.e., leading with '[' and ending ']'). so, in your example, concat actually returns "[a,b,c]", not "a,b,c". (This is easily fixed with a call to the String's substring method.)Showing
@Showing you are right! I missed that, just updated the answerMadalinemadalyn
No no no! The javadoc of Arrays.toString does not state that it will return [a, b, c]. It states this Returns a string representation of the contents of the specified array, it is only meant to be a textual representation. Do not rely on implementation details, as the Arrays.toString implementation might theoretically one day change. Never extract data out of a string representation if there are other ways.Elation
@Elation this answer is the best answer if the need is to print out array contents for logging purposes. After all, how else do you present a string array content other than the [a,b,c] format? If anything, it's still going to be pretty close to that, which is good enough for logging.Warr
this is an awful hack that relies in the string representation of one array.Isobar
O
17

You can use replace and replaceAll with regular expressions.

String[] strings = {"a", "b", "c"};

String result = Arrays.asList(strings).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", ",");

Because Arrays.asList().toString() produces: "[a, b, c]", we do a replaceAll to remove the first and last brackets and then (optionally) you can change the ", " sequence for "," (your new separator).

A stripped version (fewer chars):

String[] strings = {"a", "b", "c"};

String result = ("" + Arrays.asList(strings)).replaceAll("(^.|.$)", "").replace(", ", "," );

Regular expressions are very powerful, specially String methods "replaceFirst" and "replaceAll". Give them a try.

Odilia answered 14/10, 2011 at 8:13 Comment(1)
Is this a safe approach over time? Is the output of List.toString() guaranteed not to change in new Java versions?Isomorph
V
9

All of these other answers include runtime overhead... like using ArrayList.toString().replaceAll(...) which are very wasteful.

I will give you the optimal algorithm with zero overhead; it doesn't look as pretty as the other options, but internally, this is what they are all doing (after piles of other hidden checks, multiple array allocation and other crud).

Since you already know you are dealing with strings, you can save a bunch of array allocations by performing everything manually. This isn't pretty, but if you trace the actual method calls made by the other implementations, you'll see it has the least runtime overhead possible.

public static String join(String separator, String ... values) {
  if (values.length==0)return "";//need at least one element
  //all string operations use a new array, so minimize all calls possible
  char[] sep = separator.toCharArray();

  // determine final size and normalize nulls
  int totalSize = (values.length - 1) * sep.length;// separator size
  for (int i = 0; i < values.length; i++) {
    if (values[i] == null)
      values[i] = "";
    else
      totalSize += values[i].length();
  }

  //exact size; no bounds checks or resizes
  char[] joined = new char[totalSize];
  int pos = 0;
  //note, we are iterating all the elements except the last one
  for (int i = 0, end = values.length-1; i < end; i++) {
    System.arraycopy(values[i].toCharArray(), 0, 
      joined, pos, values[i].length());
    pos += values[i].length();
    System.arraycopy(sep, 0, joined, pos, sep.length);
    pos += sep.length;
  }
  //now, add the last element; 
  //this is why we checked values.length == 0 off the hop
  System.arraycopy(values[values.length-1].toCharArray(), 0,
    joined, pos, values[values.length-1].length());

  return new String(joined);
}
Villalba answered 14/12, 2012 at 11:59 Comment(3)
Interesting. Do you have some benchmarks showing that your implementation is faster than Google collections / Apache commons? if so, both are open source and I encourage you to submit a pull request.Was
Google's method just converts to an array and uses an iterator to append to a StringBuilder. This defaults to size 16, then resizes as it iterates. Simple, but wasteful. Apache is a little better in that they try to estimate the size of the buffer based on the size of the first element, but because it operates on plain Objects, it doesn't risk paying twice to .toString() each one. Both of these methods resort to bounds checks and resizes as they iterate. I would imagine that in the case of small arrays, Google's might be faster, and large arrays of uniform size, apache's would be...Villalba
...comparable. However, this method gets the same job done using a single precisely sized array, and doesn't even pay for if() checks within the for loop (as apache's does... google uses the Iterator interface to do something similar to what I've done, where the looping logic doesn't require an if on every iteration).Villalba
P
6

This small function always comes in handy.

public static String join(String[] strings, int startIndex, String separator) {
    StringBuffer sb = new StringBuffer();
    for (int i=startIndex; i < strings.length; i++) {
        if (i != startIndex) sb.append(separator);
        sb.append(strings[i]);
    }
    return sb.toString();
}
Primer answered 28/3, 2014 at 13:57 Comment(0)
P
6

This options is fast and clear:

  public static String join(String separator, String... values) {
    StringBuilder sb = new StringBuilder(128);
    int end = 0;
    for (String s : values) {
      if (s != null) {
        sb.append(s);
        end = sb.length();
        sb.append(separator);
      }
    }
    return sb.substring(0, end);
  }
Paraph answered 29/5, 2014 at 17:31 Comment(0)
N
3

The approach that I've taken has evolved since Java 1.0 to provide readability and maintain reasonable options for backward-compatibility with older Java versions, while also providing method signatures that are drop-in replacements for those from apache commons-lang. For performance reasons, I can see some possible objections to the use of Arrays.asList but I prefer helper methods that have sensible defaults without duplicating the one method that performs the actual work. This approach provides appropriate entry points to a reliable method that does not require array/list conversions prior to calling.

Possible variations for Java version compatibility include substituting StringBuffer (Java 1.0) for StringBuilder (Java 1.5), switching out the Java 1.5 iterator and removing the generic wildcard (Java 1.5) from the Collection (Java 1.2). If you want to take backward compatibility a step or two further, delete the methods that use Collection and move the logic into the array-based method.

public static String join(String[] values)
{
    return join(values, ',');
}

public static String join(String[] values, char delimiter)
{
    return join(Arrays.asList(values), String.valueOf(delimiter));
}

// To match Apache commons-lang: StringUtils.join(values, delimiter)
public static String join(String[] values, String delimiter)
{
    return join(Arrays.asList(values), delimiter);
}

public static String join(Collection<?> values)
{
    return join(values, ',');
}

public static String join(Collection<?> values, char delimiter)
{
    return join(values, String.valueOf(delimiter));
}

public static String join(Collection<?> values, String delimiter)
{
    if (values == null)
    {
        return new String();
    }

    StringBuffer strbuf = new StringBuffer();

    boolean first = true;

    for (Object value : values)
    {
        if (!first) { strbuf.append(delimiter); } else { first = false; }
        strbuf.append(value.toString());
    }

    return strbuf.toString();
}
Nerves answered 15/6, 2015 at 1:46 Comment(0)
R
2
public String join(String[] str, String separator){
    String retval = "";
    for (String s: str){ retval+= separator + s;}
    return retval.replaceFirst(separator, "");
}
Rihana answered 4/10, 2014 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.