Java is lacking a String “join” function

Between Java 1.0 and Java 1.3 (1996-2002 according to Wikipedia) there was no way to split strings into an array or a list.

In Java 1.4 the authors of Java saw it fit to introduce a method to split strings,

String csvData = "field1,field2,field3";
String[] fields = csvData.split(",");

However they did not introduce a method to “join” strings! Even in Java 7 there is no way to do this, e.g. via a static String.join method (2002-now).

OK I realize this is not “rocket science”, and I appreciate it exists in various versions in various third-party libraries, but still, it’s something every program needs to do at some point, it’s annoying to have to re-define it or think about it for each application.

For example, in one project I was working on in the last 6 months, such a function was created, and then had a bug! (OK but to be fair, that was not the only bug in the application!)

Come on, I mean this is a totally trivial function, totally necessary, available in all scripting lanuags why is it still not in Java?!

(P.S. Want to see an “enterprise java” solution to this problem? Check out the number of methods on this class)

2 Responses to “Java is lacking a String “join” function”

  1. Robin Salih Says:

    It does seem to be a bit of an oversight. In .net you do get this method: http://msdn.microsoft.com/en-us/library/system.string_methods(v=VS.100).aspx. Like you say, trivial to write your own, but as it is so general purpose it should be part of the language.

    I imagine in the Java world, every codebase has it’s own version of Join, with slightly differing signatures!

  2. Robert Says:

    Amusingly (depending on POV), there’s also no Base64 en/decoding support in the JDK (neither in JEE).

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

For inserting HTML or XML please remember to use &lt; instead of <