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”].
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:
String with empty strings: geeks,, for,,, geeks,, noida,,, classes String with empty strings removed: [geeks, for, geeks, noida, classes]
String with empty strings: Hello..$.$ everyone..$& $ what's up..? String with empty strings removed: [Hello, $, $ everyone, $& $ what's up, ?]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.