Splitter omitEmptyStrings() method | Guava | Java
The method 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”].
Syntax:
public Splitter omitEmptyStrings()
Return Value: This method returns a splitter with the desired configuration.
Note: 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.
Below examples illustrate the working of omitEmptyStrings() method:
Example 1:
// Java code to show implementation of // omitEmptyStrings() method // of Guava's Splitter Class import com.google.common.base.Splitter; import java.util.List; class GFG { // Driver's code public static void main(String[] args) { // Creating a string variable String str = "geeks,, for,,, geeks,, noida,,, classes"; System.out.println("String with empty strings: \n" + str); // Using omitEmptyStrings() method. // Two delimiters sometimes occur right next // to each other. This means an empty entry. // But often in splitting, we don't want // to keep empty entries. List<String> myList = Splitter.on(', '). trimResults().omitEmptyStrings().splitToList(str); System.out.println("\nString with empty" + " strings removed: \n" + myList); } } |
String with empty strings: geeks,, for,,, geeks,, noida,,, classes String with empty strings removed: [geeks, for, geeks, noida, classes]
Example 2:
// Java code to show implementation of // omitEmptyStrings() method // of Guava's Splitter Class import com.google.common.base.Splitter; import java.util.List; class GFG { // Driver's code public static void main(String[] args) { // Creating a string variable String str = "Hello..$.$ everyone..$& $ what's up..?"; System.out.println("String with empty strings: \n" + str); // Using omitEmptyStrings() method. // Two delimiters sometimes occur right next // to each other. This means an empty entry. // But often in splitting, we don't want // to keep empty entries. List<String> myList = Splitter.on('.'). trimResults().omitEmptyStrings().splitToList(str); System.out.println("\nString with empty" + " strings removed: \n" + myList); } } |
String with empty strings: Hello..$.$ everyone..$& $ what's up..? String with empty strings removed: [Hello, $, $ everyone, $& $ what's up, ?]
Recommended Posts:
- Splitter trimResults() method | Guava | Java
- Splitter limit() method | Guava | Java
- Splitter splitToList() method | Guava | Java
- Splitter fixedLength() method | Guava | Java
- Splitter withKeyValueSeparator(char separator) method | Guava | Java
- Splitter Class | Guava | Java
- Java Guava | Chars.max() method with Examples
- Java Guava | Doubles.min() method with Examples
- Java Guava | Floats.max() method with Examples
- Java Guava | Bytes.contains() method with Examples
- Java Guava | Doubles.max() method with Examples
- Java Guava | gcd(int a, int b) method of IntMath Class
- Java Guava | Chars.contains() method with Examples
- Java Guava | Longs.min() method with Examples
- Java Guava | Chars.min() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



