Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 5015163
Votes 28
Synopsis (str) String merge/join that is the inverse of String.split()
Category java:classes_lang
Reported Against 1.4.2
Release Fixed
State 7-Fix in Progress, request for enhancement
Priority: 3-Medium
Related Bugs 6538167
Submit Date 17-MAR-2004
Description


A DESCRIPTION OF THE REQUEST :
The class java.lang.String lacks a facility to merge a String array to a single String with support for a delimiter that's inserted between the array elements. This facility should be the opposite of java.lang.String.split .

JUSTIFICATION :
The String merge/join facility is very common thing and can be used in many situations. It would make the life of the majority of Java developers easier.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The best way to provide a String merge/join facility would be to implement a new String constructor with two parameters: a String array and a delimiter.

public String(String[] tokens, String delimiter)
{
// The implementation is left as an exercise to the reader ;-)
}
ACTUAL -
The following pseudo code shows how the new constructor should work :

String[] tokens = new String[]{"Hello", "World!"};

String greeting = new String(tokens, ", ");

System.out.println(greeting);

The output of the code snippet should be: Hello, World!

CUSTOMER SUBMITTED WORKAROUND :
A workaround would be to implement a helper mewthod that does the merge.
(Incident Review ID: 240506) 
======================================================================
Posted Date : 2005-07-27 04:37:46.0
Work Around
N/A
Evaluation
I once independently suggested adding a join facility.
Given that this simple functionality gets reinvented, and
that split and join are often used in tandem, and that
perl, which was perhaps the inspiration for split, also has join,
there is strong motivation for providing join.
However, I don't think adding a constructor is a good idea.
The intent is not obvious enough.

I would favor a static method analogous to perl's

public static String join(String delimiter, String... components)

or perhaps more abstractly

public static String join(CharSequence delimiter, 
                          CharSequence... components)

This even succeeds in copying the vargarity of perl join.

  xxxxx@xxxxx   2005-04-13 20:40:04 GMT
Please refer to the forum thread on this topic for several design suggestions and two different suggested fixes.

Contribution-forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=10624
Posted Date : 2005-12-21 23:46:08.0
Comments
  
  Include a link with my name & email   

Submitted On 05-MAR-2007
miiken
This is in progress for 3 years now...
PLEASE add this to next release


Submitted On 28-MAR-2007
This is a really simple enhancement that is done over and over in many external libraries.  It's about time that it gets done...


Submitted On 03-JUL-2008
Please add this feature!


Submitted On 03-JUL-2008
http://forum.java.sun.com/thread.jspa?threadID=5310610&tstart=0


Submitted On 17-JUL-2008
Sch104
Yes it is a simple feature that is done in many external libraries.  It would be quite simple to add and there really isn't a reason to not have it.


Submitted On 02-OCT-2008
gforman
Don't just accept a list of Strings, make it more general:

join(CharSequence delim, **Collection/Iterable** list)

.toString() will be called on each object in the list.


Submitted On 22-DEC-2008
tstyblo
    public static <T> String joinCollection (final Collection<T> objs, String delimiter) 
    {
        if (objs == null || objs.isEmpty())
            return "";
        Iterator<T> iter = objs.iterator();
        StringBuffer buffer = new StringBuffer(iter.next().toString());
        while (iter.hasNext())
            buffer.append(delimiter).append(iter.next().toString());
        return buffer.toString();
    }


Submitted On 19-JAN-2009
ivarru
I propose the following implementation, which is both more general and slightly optimized w.r.t. joining one element collections:


    public static String join(Object delimiter, Iterable<?> elements) {
    	final Iterator<?> it;
        if (elements == null || !(it = elements.iterator()).hasNext()) {
        	return "";
        }
        final String first = it.next().toString();
        if (!it.hasNext()) {
        	return first;
        }
		final StringBuilder result = new StringBuilder(first);
        do {
        	result.append(delimiter)
        	      .append(it.next());
        } while (it.hasNext());
        return result.toString();
    }

    public static String join (Object delimiter, Object... elements) {
    	return join(delimiter, Arrays.asList(elements));
    }


Submitted On 29-MAY-2009
MartinHilpert
We badly need high performance string concatenation in Java!



PLEASE NOTE: JDK6 is formerly known as Project Mustang