java.lang.Object
de.cuioss.tools.string.Joiner
Inspired by Googles Joiner.
It uses internally the String.join(CharSequence, Iterable)
implementation of java and provides a guava like wrapper. It focuses on the
simplified Joining and omits the Map based variants.
Usage
assertEquals("key=value", Joiner.on('=').join("key", "value"));
assertEquals("key=no value", Joiner.on('=').useForNull("no value").join("key", null));
assertEquals("key", Joiner.on('=').skipNulls().join("key", null));
assertEquals("key", Joiner.on('=').skipEmptyStrings().join("key", ""));
assertEquals("key", Joiner.on('=').skipBlankStrings().join("key", " "));
Migrating from Guava
In order to migrate for most case you only need to replace the package name on the import.
Changes to Guavas-Joiner
In case of content to be joined containing null-values and not set to
skip nulls, skipNulls() it does not throw an
NullPointerException but writes "null" for each null element.
You can define a different String by calling useForNull(String)
In addition to skipEmptyStrings() it provides a variant
skipBlankStrings()
- Author:
- Oliver Wolff
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Joineron(char separator) Returns a Joiner that uses the given fixed string as a separator.static JoinerReturns a Joiner that uses the given fixed string as a separator.useForNull(String nullText)
-
Constructor Details
-
Joiner
public Joiner()
-
-
Method Details
-
on
Returns a Joiner that uses the given fixed string as a separator. For example,Joiner.on("-").join("foo", "bar")returns a String "foo-bar"- Parameters:
separator- the literal, nonempty string to recognize as a separator- Returns:
- a
Joiner, with default settings, that uses that separator
-
on
Returns a Joiner that uses the given fixed string as a separator. For example,Joiner.on('-').join("foo", "bar")returns a String "foo-bar"- Parameters:
separator- the literal, nonempty string to recognize as a separator- Returns:
- a
Joiner, with default settings, that uses that separator
-
useForNull
- Parameters:
nullText- to be used as substitution fornullelements- Returns:
- a joiner with the same behavior as this one, except automatically
substituting
nullTextfor any provided null elements.
-
skipNulls
- Returns:
- a joiner with the same behavior as this one, except automatically skipping null-values
-
skipEmptyStrings
- Returns:
- a joiner with the same behavior as this one, except automatically skipping String-values that evaluate to an empty String
-
skipBlankStrings
- Returns:
- a joiner with the same behavior as this one, except automatically
skipping String-values that evaluate to a blank String as defined
within
MoreStrings.isBlank(CharSequence)
-
join
- Parameters:
parts- to be joined- Returns:
- a string containing the string representation of each of
parts, using the previously configured separator between each.
-
join
- Parameters:
parts- to be joined- Returns:
- a string containing the string representation of each of
parts, using the previously configured separator between each.
-
join
- Parameters:
parts- to be joined- Returns:
- a string containing the string representation of each of
parts, using the previously configured separator between each.
-