A method to reverse effect of java String.split()? [duplicate] A method to reverse effect of java String.split()? [duplicate] java java

A method to reverse effect of java String.split()? [duplicate]


There's no method in the JDK for this that I'm aware of. Apache Commons Lang has various overloaded join() methods in the StringUtils class that do what you want.


There has been an open feature request since at least 2009. The long and short of it is that it will part of the functionality of JDK 8's java.util.StringJoiner class. http://download.java.net/lambda/b81/docs/api/java/util/StringJoiner.html

Here is the Oracle issue if you are interested.http://bugs.sun.com/view_bug.do?bug_id=5015163

Here is an example of the new JDK 8 StringJoiner on an array of String

String[] a = new String[]{"first","second","third"};StringJoiner sj = new StringJoiner(",");for(String s:a) sj.add(s);System.out.println(sj); //first,second,third

A utility method in String makes this even simpler:

String s = String.join(",", stringArray);


You can sneak this functionality out of the Arrays utility package.

import java.util.Arrays;...    String delim = ":",            csv_record = "Field0:Field1:Field2",            fields[] = csv_record.split(delim);    String rebuilt_record = Arrays.toString(fields)            .replace(", ", delim)            .replaceAll("[\\[\\]]", "");