Package io.cui.tools.string
Class Joiner
- java.lang.Object
-
- io.cui.tools.string.Joiner
-
public final class Joiner extends Object
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 anNullPointerExceptionbut writes "null" for eachnullelement. You can define a different String by callinguseForNull(String)In addition to
skipEmptyStrings()it provides a variantskipBlankStrings()- Author:
- Oliver Wolff
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringjoin(Iterable<?> parts)Stringjoin(Object... parts)Stringjoin(Iterator<?> parts)static Joineron(char separator)Returns a Joiner that uses the given fixed string as a separator.static Joineron(String separator)Returns a Joiner that uses the given fixed string as a separator.JoinerskipBlankStrings()JoinerskipEmptyStrings()JoinerskipNulls()JoineruseForNull(String nullText)
-
-
-
Method Detail
-
on
public static Joiner on(String separator)
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
public static Joiner on(char separator)
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
public Joiner useForNull(String nullText)
- 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
public Joiner skipNulls()
- Returns:
- a joiner with the same behavior as this one, except automatically skipping null-values
-
skipEmptyStrings
public Joiner skipEmptyStrings()
- Returns:
- a joiner with the same behavior as this one, except automatically skipping String-values that evaluate to an empty String
-
skipBlankStrings
public Joiner 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
public String join(Iterable<?> parts)
- Parameters:
parts- to be joined- Returns:
- a string containing the string representation of each of
parts, using the previously configured separator between each.
-
join
public String join(Iterator<?> parts)
- Parameters:
parts- to be joined- Returns:
- a string containing the string representation of each of
parts, using the previously configured separator between each.
-
-