If the individual strings might themselves include a comma followed by a space, then this would not be feasible. Multiple String[] arrays could map to the same flat String, and an inverse function would not exist.
However, you note in a comment that your strings cannot include the comma separator. You can split the flat string back into the original substrings, using ", " (comma,space) as a separator.
APIs that support this include the standard Java String.split()
method, and Guava's Splitter
class.
Here's an example with Java's split()
method:
public static void main(String[] args) {
String[] strs = new String[] { "Foo", "Bar", "Baz" };
String joined = Arrays.toString( strs );
String joinedMinusBrackets = joined.substring( 1, joined.length() - 1);
// String.split()
String[] resplit = joinedMinusBrackets.split( ", ");
for ( String s : resplit ) {
System.out.println( s );
}
}
And here's an example with Guava's Splitter
class:
public static void main(String[] args) {
String[] strs = new String[] { "Foo", "Bar", "Baz" };
String joined = Arrays.toString( strs );
String joinedMinusBrackets = joined.substring( 1, joined.length() - 1);
// Guava Splitter class
List<String> resplitList = Splitter.on( ", " ).splitToList( joinedMinusBrackets );
for ( String s : resplitList ) {
System.out.println( s );
}
}