StringJoiner
is used to construct a sequence of characters separated
by a delimiter and optionally starting with a supplied prefix
and ending with a supplied suffix.
Prior to adding something to the StringJoiner
, its
sj.toString()
method will, by default, return prefix + suffix
.
However, if the setEmptyValue
method is called, the emptyValue
supplied will be returned instead. This can be used, for example, when
creating a string using set notation to indicate an empty set, i.e.
"{}"
, where the prefix
is "{"
, the
suffix
is "}"
and nothing has been added to the
StringJoiner
.
API Note
The String "[George:Sally:Fred]"
may be constructed as follows:
StringJoiner sj = new StringJoiner(":", "[", "]");
sj.add("George").add("Sally").add("Fred");
String desiredString = sj.toString();
A StringJoiner
may be employed to create formatted output from a
java.
using
java.
. For example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
String commaSeparatedNumbers = numbers.stream()
.map(i -> i.toString())
.collect(Collectors.joining(", "));
java.util.stream.Collectors#joining(CharSequence)
, java.util.stream.Collectors#joining(CharSequence, CharSequence, CharSequence)
Modifier and Type | Field and Description |
---|---|
private final String | |
private String[] | elts
Contains all the string components added so far. |
private static final String[] | |
private String | emptyValue
When overridden by the user to be non-null via |
private static final JavaLangAccess | |
private int | len
Total length in chars so far, excluding prefix and suffix. |
private final String | |
private int | size
The number of string components added so far. |
private final String |
Access | Constructor and Description |
---|---|
public | StringJoiner(CharSequence
the sequence of characters to be used between each
element added to the delimiter)StringJoiner valueConstructs a |
public | StringJoiner(CharSequence
the sequence of characters to be used between each
element added to the delimiter, CharSequence StringJoiner the sequence of characters to be used at the beginning prefix, CharSequence the sequence of characters to be used at the end suffix)Constructs a |
Modifier and Type | Method and Description |
---|---|
public StringJoiner | Returns: a reference to thisStringJoiner The element to add newElement)Adds a copy of the given |
private int | |
private void | |
public int | Returns: the length of the current value ofStringJoiner Returns the length of the |
public StringJoiner | Returns: ThisStringJoiner The other)StringJoiner whose contents should be merged
into this oneAdds the contents of the given |
public StringJoiner | Returns: thisStringJoiner itself so the calls may be chainedthe characters to return as the value of an empty
emptyValue)StringJoiner Sets the sequence of characters to be used when determining the string
representation of this |
public String | Returns: the string representation of thisStringJoiner Overrides java. Returns the current value, consisting of the |
delimiter | back to summary |
---|---|
private final String delimiter |
elts | back to summary |
---|---|
private String[] elts Contains all the string components added so far. |
EMPTY_STRING_ARRAY | back to summary |
---|---|
private static final String[] EMPTY_STRING_ARRAY |
emptyValue | back to summary |
---|---|
private String emptyValue When overridden by the user to be non-null via |
JLA | back to summary |
---|---|
private static final JavaLangAccess JLA |
len | back to summary |
---|---|
private int len Total length in chars so far, excluding prefix and suffix. |
prefix | back to summary |
---|---|
private final String prefix |
size | back to summary |
---|---|
private int size The number of string components added so far. |
suffix | back to summary |
---|---|
private final String suffix |
StringJoiner | back to summary |
---|---|
public StringJoiner(CharSequence delimiter) Constructs a
|
StringJoiner | back to summary |
---|---|
public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) Constructs a
|
add | back to summary |
---|---|
public StringJoiner add(CharSequence newElement) Adds a copy of the given
|
checkAddLength | back to summary |
---|---|
private int checkAddLength(int oldLen, int inc) |
compactElts | back to summary |
---|---|
private void compactElts() |
length | back to summary |
---|---|
public int length() Returns the length of the
|
merge | back to summary |
---|---|
public StringJoiner merge(StringJoiner other) Adds the contents of the given A If the other
|
setEmptyValue | back to summary |
---|---|
public StringJoiner setEmptyValue(CharSequence emptyValue) Sets the sequence of characters to be used when determining the string
representation of this
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns the current value, consisting of the |