Joining of CharSequence Objects
One operation commonly performed on a sequence of strings is to format them so that each string is separated from the next one by a delimiter. For example, given the following sequence of strings:
“2014”
“January”
“11”
we wish to format them so that individual strings are separated by the delimiter “/”:
“2014/January/11”
The following static methods in the String class can be used for this purpose:
static String join(CharSequence delimiter, CharSequence… elements)
static String join(CharSequence delimiter,
Iterable<? extends CharSequence> elements)
Both static methods return a new String composed of copies of the CharSequence elements joined together with a copy of the specified CharSequence delimiter. Thus the resulting string is composed of text representations of the elements separated by the text representation of the specified delimiter.
If either delimiter is null or elements is null, a NullPointerException is thrown. If both are null, the method call is ambiguous.
If an element in elements is null, the string “null” is added as its text representation.
Note that both the individual strings and the delimiter string are of type CharSequence. The examples in this section use String and StringBuilder objects that implement the CharSequence interface (p. 444).
An Iterable provides an iterator to iterate over its elements. The following examples use an ArrayList (§12.5, p. 657) that implements the Iterable interface. The second join() method is then able to iterate over the Iterable using the iterator. This method will accept only an Iterable whose elements are of type CharSequence or are subtypes of CharSequence.
The first example shows joining of String objects. The first join() method is called in this case.
// (1) Joining individual String objects:
String dateStr = String.join(“/”, “2014”, “January”, “11”);
System.out.println(dateStr); // 2014/January/11
The second example shows joining of elements in a StringBuilder array. Again the first join() method is called, with the array being passed as the second parameter.
// (2) Joining elements in a StringBuilder array:
StringBuilder left = new StringBuilder(“Left”);
StringBuilder right = new StringBuilder(“Right”);
StringBuilder[] strBuilders = { left, right, left };
String march = String.join(“–>”, strBuilders);
System.out.println(march); // Left–>Right–>Left
The third example shows joining of elements in an ArrayList of StringBuilder. The second join() method is called, with the ArrayList being passed as the second parameter. Note that some of the elements of the ArrayList are null.
// (3) Joining elements in a StringBuilder list:
ArrayList<StringBuilder> sbList = new ArrayList<>();
sbList.add(right); sbList.add(null); sbList.add(left); sbList.add(null);
String resultStr = “[” + String.join(“, “, sbList) + “]”;
System.out.println(resultStr); // [Right, null, Left, null]
The last example shows joining of elements in an ArrayList of CharSequence. Again the second join() method is called, with the ArrayList being passed as the second parameter. Note that elements of the ArrayList are String and StringBuilder objects that are also of type CharSequence.
// (4) Joining elements in a CharSequence list:
ArrayList<CharSequence> charSeqList = new ArrayList<>();
charSeqList.add(right); charSeqList.add(left); // Add StringBuilder objects.
charSeqList.add(“Right”); charSeqList.add(“Left”); // Add String objects.
String resultStr2 = “<” + String.join(“; “, charSeqList) + “>”;
System.out.println(resultStr2); // <Right; Left; Right; Left>
Leave a Reply