Class Splitter

java.lang.Object
de.cuioss.tools.string.Splitter

public final class Splitter extends Object
Derived from Googles Splitter.

It uses internally the String.split(String) implementation of java and provides a guava like wrapper. This results in an implicit splitting of the whole String compared to the lazy / deferred splitting of guava. It focuses and RegEx-based splitting and omits the fixedLength and Map based variants.

Migrating from Guava

In order to migrate for most case you only need to replace the package name on the import. A major different is that the split method provided is splitToList(String), the variant split() is replaced by it completely.

Changes to Guavas-Splitter

It is quite similar to the guava variant but behaves a little different in certain details, especially in the context of limit(int) and trimResults(), omitEmptyStrings(). While guavas version will filter the limit elements after the application of omit / trim, this version will do it the other way round, resulting in a different result compared to the guava version.

Author:
Oliver Wolff
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Usually the separator will be pre-processed before being passed to String.split(String).
    limit(int maxItems)
    Returns a splitter that behaves equivalently to this splitter but stops splitting after it reaches the limit.
    Returns a splitter that behaves equivalently to this splitter, but automatically omits empty strings from the results.
    static Splitter
    on(char separator)
    Returns a splitter that uses the given fixed string as a separator.
    static Splitter
    on(String separator)
    Returns a splitter that uses the given fixed string as a separator.
    splitToList(String sequence)
    Splits sequence into string components and returns them as an immutable list.
    Returns a splitter that behaves equivalently to this splitter, but automatically removes leading and trailing whitespace from each returned substring.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • on

      public static Splitter on(String separator)
      Returns a splitter that uses the given fixed string as a separator. For example, Splitter.on(", ").split("foo, bar,baz") returns an iterable containing ["foo", "bar,baz"].
      Parameters:
      separator - the literal, nonempty string to recognize as a separator
      Returns:
      a splitter, with default settings, that recognizes that separator
    • on

      public static Splitter on(char separator)
      Returns a splitter that uses the given fixed string as a separator. For example, Splitter.on(", ").split("foo, bar,baz") returns an iterable containing ["foo", "bar,baz"].
      Parameters:
      separator - the literal, nonempty string to recognize as a separator
      Returns:
      a splitter, with default settings, that recognizes that separator
    • omitEmptyStrings

      Returns a splitter that behaves equivalently to this splitter, but automatically omits empty strings from the results. For example, Splitter.on(',').omitEmptyStrings().split(",a,,,b,c,,") returns an iterable containing only ["a", "b", "c"].

      If either trimResults option is also specified when creating a splitter, that splitter always trims results first before checking for emptiness. So, for example, Splitter.on(':').omitEmptyStrings().trimResults().split(": : : ") returns an empty iterable.

      Returns:
      a splitter with the desired configuration
    • doNotModifySeparatorString

      Usually the separator will be pre-processed before being passed to String.split(String). This is needed to mask special characters that will harm Pattern.compile(String). If you want to disable this behavior and take care for your self you can change this method by calling this method.
      Returns:
      a splitter with the desired configuration
    • trimResults

      Returns a splitter that behaves equivalently to this splitter, but automatically removes leading and trailing whitespace from each returned substring. For example, Splitter.on(',').trimResults().split(" a, b ,c ") returns an iterable containing ["a", "b", "c"].
      Returns:
      a splitter with the desired configuration
    • limit

      public Splitter limit(int maxItems)
      Returns a splitter that behaves equivalently to this splitter but stops splitting after it reaches the limit. The limit defines the maximum number of items returned by the iterator, or the maximum size of the list returned by splitToList(java.lang.String).

      For example, Splitter.on(',').limit(3).split("a,b,c,d") returns an iterable containing ["a", "b", "c,d"]. When omitting empty strings, the omitted strings do not count. Hence, Splitter.on(',').limit(3).omitEmptyStrings().split("a,,,b,,,c,d") returns an iterable containing ["a", "b", "c,d". When trim is requested, all entries are trimmed, including the last. Hence Splitter.on(',').limit(3).trimResults().split(" a , b , c , d ") results in ["a", "b", "c , d"].

      Parameters:
      maxItems - the maximum number of items returned
      Returns:
      a splitter with the desired configuration
    • splitToList

      public List<String> splitToList(String sequence)
      Splits sequence into string components and returns them as an immutable list.
      Parameters:
      sequence - the sequence of characters to split
      Returns:
      an immutable list of the segments split from the parameter