Splitter Class | Guava | Java
Last Updated :
31 Jan, 2019
Guava’s Splitter Class provides various methods to handle splitting operations on string, objects, etc. It extracts non-overlapping substrings from an input string, typically by recognizing appearances of a separator sequence. This separator can be specified as a single character, fixed string, regular expression or CharMatcher instance.
Declaration: Following is the declaration for com.google.common.base.Splitter class:
@GwtCompatible(emulated = true)
public final class Splitter
extends Object
The following table gives a brief summary about the methods of Guava’s Splitter class:
Example:
import com.google.common.base.Splitter;
class GFG {
public static void main(String[] args)
{
System.out.println(Splitter.on( ',' )
.trimResults()
.omitEmptyStrings()
.split("GeeksforGeeks ,is, the,
best, website, to, prepare, for , interviews"));
}
}
|
Output:
[GeeksforGeeks, is, the, best, website, to, prepare, for, interviews]
Some other methods provided by the Splitter class are:
Example:
import com.google.common.base.Splitter;
import java.util.List;
class GFG {
public static void main(String[] args)
{
String str= "Hello, GFG, What's up ?" ;
List<String> myList = Splitter.on( ',' ).splitToList(str);
for (String ele : myList) {
System.out.println(ele);
}
}
}
|
Output:
Hello
GFG
What's up ?
Reference: Google Guava
Share your thoughts in the comments
Please Login to comment...