Guava – Splitter Class

  • Post author:
  • Post category:Guava
  • Post comments:1 Comment
splitting operations

Splitter provides various methods to handle splitting operations on string, objects, etc.

Class Declaration

Following is the declaration for com.google.common.base.Splitter class โˆ’

@GwtCompatible(emulated = true)
public final class Splitter
   extends Object

Class Methods

Sr.NoMethod & Description
1static Splitter fixedLength(int length)Returns a splitter that divides strings into pieces of the given length.
2Splitter limit(int limit)Returns a splitter that behaves equivalently to this splitter but stops splitting after it reaches the limit.
3Splitter omitEmptyStrings()Returns a splitter that behaves equivalently to this splitter, but automatically omits empty strings from the results.
4static Splitter on(char separator)Returns a splitter that uses the given single-character separator.
5static Splitter on(CharMatcher separatorMatcher)Returns a splitter that considers any single character matched by the given CharMatcher to be a separator.
6static Splitter on(Pattern separatorPattern)Returns a splitter that considers any subsequence matching pattern to be a separator.
7static Splitter on(String separator)Returns a splitter that uses the given fixed string as a separator.
8static Splitter onPattern(String separatorPattern)Returns a splitter that considers any subsequence matching a given pattern (regular expression) to be a separator.
9Iterable<String> split(CharSequence sequence)Splits sequence into string components and makes them available through an Iterator, which may be lazily evaluated.
10List<String> splitToList(CharSequence sequence)Splits sequence into string components and returns them as an immutable list.
11Splitter trimResults()Returns a splitter that behaves equivalently to this splitter, but automatically removes leading and trailing whitespace from each returned substring; equivalent to trimResults(CharMatcher.WHITESPACE).
12Splitter trimResults(CharMatcher trimmer)Returns a splitter that behaves equivalently to this splitter, but removes all leading or trailing characters matching the given CharMatcher from each returned substring.
13Splitter.MapSplitter withKeyValueSeparator(char separator)Returns a MapSplitter which splits entries based on this splitter, and splits entries into keys and values using the specified separator.
14Splitter.MapSplitter withKeyValueSeparator(Splitter keyValueSplitter)Returns a MapSplitter which splits entries based on this splitter, and splits entries into keys and values using the specified key-value splitter.
15Splitter.MapSplitter withKeyValueSeparator(String separator)Returns a MapSplitter which splits entries based on this splitter, and splits entries into keys and values using the specified separator.

Methods Inherited

This class inherits methods from the following class โˆ’

  • java.lang.Object

Example of Splitter Class

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

import com.google.common.base.Splitter;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testSplitter();
   }

   private void testSplitter() {
      System.out.println(Splitter.on(',')
         .trimResults()
         .omitEmptyStrings()
         .split("the ,quick, ,brown, fox, jumps, over, the, lazy, little dog."));
   }
}

Verify the Result

Compile the class using javac compiler as follows โˆ’

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

[the, quick, brown, fox, jumps, over, the, lazy, little dog.]

Previous Page:-Click Here

This Post Has One Comment

Leave a Reply