Skip to main content

String join method

7 replies [Last post]
martin_woolstenhulme
Offline
Joined: 2007-09-10

It was a welcome introduction when Java added regular expression and String manipulation functions like split() in 1.4 (I think it was then), but why wasn't a join function added to complement split()?

It's easy enough to implement, but it would be nice to be apart of the API with two possible method signatures one accepting String arrays and the other accepting String collections as parameters. I can't be the first one to bring this up, so if someone can set me straight, please do.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
martinoc
Offline
Joined: 2006-05-05

My advice would be to make your implementation of this method available under some open source license, package it up in a jar and include it in the classpath of your projects. Try to gain some traction, and, if enough people find it useful, it will become defacto. Post the implementation to the relevant JSR mailing list.

A further step would be to contribute it to the open JDK project, but it would have to get through the JSR process first in order to make it in to Java.

martin_woolstenhulme
Offline
Joined: 2007-09-10

Thanks, I can see there is a long process to this, but you've provided a road map.

martinoc
Offline
Joined: 2006-05-05

Strings may be concatinated using the + operator
if you need a method call to concatinate two string objects, then you can use the String.concat() method.

example

String s1 = "Hello";
String s2 = " World";
String s3 = s1.concat(s2);

martin_woolstenhulme
Offline
Joined: 2007-09-10

Thank you for your reply.

I'm well aware of String concatenation, but I was talking about the situation where you have a String[] or Collection, and you want to put them all together separated by a common delimiter and returned as a single String object. It's really the inverse operation of the split() method. Look at its method signature and compare it to the method signatures I'm suggesting.

public String[] split(String regex)

public static String join(String[] strings, String delimiter);
public static String join(Collection strings, String delimiter);

I may have just answered my question by writing out this response, because you would have to make the join() method static in order to implement it in the String class. The only other choice would be to make constructors with similar parameters, but then the overt call to join() is no longer used. But I don't see why the addition of a static method would be prohibitive. Is there any way to do this with String.format()? Have a format string that specifies a pattern to use in between all elements of the variable length parameter list.

The reason I brought this up is when I was first introduced to Perl I learned the split() method and the join() method, so I supposed they should always go hand-in-hand. We have a case here in Java where we have split, but no join. Again, it's not a difficult thing to implement, but it would be convenient to have it in the API.

tarbo
Offline
Joined: 2006-12-18

You're right that this is "missing". In a sense, Arrays.toString() is a subset implementation (using commas), but it's not really useful in this case. As far as I know, there is no clean or direct way to do this; you'll need to write your own method for it.

If you need this for your project, or it just happens to be handy, you can use such a method yourself in the meantime. Either you write it as a static method somewhere and import it, or you can modify java.lang.String to have an extra method.

[code] public static T join(Iterable src, CharSequence pattern, T dst) throws IOException {
Iterator it = src.iterator();
if (it.hasNext()) {
dst.append(it.next());
}
while (it.hasNext()) {
dst.append(pattern).append(it.next());
}
return dst;
}

public static String join(Iterable src, CharSequence pattern) {
try {
return join(src, pattern, new StringBuilder()).toString();
} catch (IOException excpt) {
throw new Error("StringBuilder should not throw IOExceptions!");
}
}[/code]
Obvious disadvantages are that you would need to ship this modified class with your projects, that some VMs might object to "core" classes being modified or not load them, and that every new modification made to String from the community's end will need to be copy-pasted in.

Hope this helps.

martin_woolstenhulme
Offline
Joined: 2007-09-10

Thanks for your reply, your definition is much more generic. May I use it as I pursue this further?

Also, does this definition allow for String[] without having to first convert it into an Iterable, I guess what I'm asking is do arrays of objects implement Iterable?

Thanks again.

tarbo
Offline
Joined: 2006-12-18

Sure, use as you see fit. ;)

No, arrays don't work this way. While you can use arrays as the source of a for-each loop, they are not Iterable as such. You can, however, wrap them in an iterable collection with [i]List Arrays.asList(T... src)[/i] before supplying them to your method, or you can create delegating methods, whichever you prefer.