Guava Splitter.omitEmptyStrings() method in Java
This post will discuss about Guava Splitter.omitEmptyStrings() method in Java.
Sometimes, you might need to split a string into smaller segments based on a separator or pattern. However, the standard Java String.split() method does not handle empty strings well. For example, if you split the string "a,b,c" by the comma character, you will get an array of six elements: ["a", "", "", "b", "", "", "c"]. The empty strings are not omitted, but included in the result. This can be problematic if you want to ignore the empty strings and only get the non-empty segments. You might have to write extra logic to filter out the empty strings, which can be tedious and error-prone.
Fortunately, there is a better way to split strings in Java using Splitter class by Guava. This class provides a fluent and flexible API to create and manipulate splitters, which are objects that can split strings into segments. In this post, we will show you how to use the Guava Splitter.omitEmptyStrings() method, which is one of the methods that you can use to create and configure splitters.
1. Usage of Splitter.omitEmptyStrings() method
The Guava Splitter.omitEmptyStrings() method is an instance method that returns a splitter that behaves equivalently to this splitter, but automatically omits empty strings from the results. The syntax of the Splitter.omitEmptyStrings() method is as follows:
|
1 |
public Splitter omitEmptyStrings() |
The return value is a splitter with the desired configuration. There are two steps to create and use a splitter with omitEmptyStrings():
- Create a splitter by calling the static method
Splitter.on()with a separator character or pattern as a parameter. This method returns an instance of Splitter that can split strings by the given separator. - Call the instance method
omitEmptyStrings()on the splitter to return a new splitter that omits empty strings from the results. You can also chain other methods on the splitter to further customize its behavior, such aslimit(),trimResults(), orwithKeyValueSeparator(). - Call the instance method
split()on the splitter with a string as a parameter. This method returns an iterable of segments split from the string by the splitter.
For example, suppose you have a string separated by commas and if you want to split this string into segments by commas and omit empty strings, you can use the following code:
|
1 2 3 4 5 6 7 8 9 10 |
import com.google.common.base.Splitter; class Main { public static void main(String[] args) { String str = "a,,b,,c,,"; Splitter splitter = Splitter.on(',').omitEmptyStrings(); Iterable<String> segments = splitter.split(str); System.out.println(segments); // [a, b, c] } } |
Note that if trimResults() option is also specified when creating a splitter, that splitter always trims results first before checking for emptiness. For example, the following code returns an empty iterable.
|
1 2 3 4 5 6 7 8 9 10 |
import com.google.common.base.Splitter; class Main { public static void main(String[] args) { String str = " : : : "; Splitter splitter = Splitter.on(':').omitEmptyStrings().trimResults(); Iterable<String> segments = splitter.split(str); System.out.println(segments); // [] } } |
2. Benefits and drawbacks of using the omitEmptyStrings() method
The omitEmptyStrings() method has some benefits and drawbacks when compared with other methods of splitting strings in Java.
- One benefit is that it simplifies the logic of splitting strings and handling empty strings. You don’t have to write any loops or conditions to filter out empty strings from the results. You just need to chain one method call on the splitter and get the desired result.
- Another benefit is that it is consistent and compatible with other methods of the
Splitterclass. You can useomitEmptyStrings()with any separator character or pattern, and with any other configuration options of the splitter. You can also use it with any type of input string, whether it is null, empty, or non-empty. - Using Guava library is a trade-off, as it adds an extra dependency to your project. This means that you might need to include it as an external dependency if you are not already using it. This could make your project bigger and more complex.
- Another drawback is that it might not suit your specific needs or preferences. Sometimes, you might want to keep or handle empty strings differently than omitting them. For example, you might want to replace them with default values, throw exceptions, etc. For these cases, you might have to write your own logic or use another method of splitting strings.
3. Alternatives to omitEmptyStrings() method
One alternative is to use a third-party library that provides a similar functionality as the omitEmptyStrings() method. For example, you can use the Apache Commons Lang library, which has a StringUtils.split() method that works in a similar way as the omitEmptyStrings() method. You can use it as follows:
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; class Main { public static void main(String[] args) { String str = "a,,b,,c,,"; String[] segments = StringUtils.split(str, ','); System.out.println(Arrays.toString(segments)); } } |
The advantage of using this alternative is that it is simple and convenient to use. It also supports null and empty input strings. The disadvantage of using this alternative is that it requires an extra dependency on the Apache Commons Lang library. It also does not support other configuration options of the splitter, such as limit(), trimResults(), or withKeyValueSeparator().
4. Conclusion
In this post, we have covered how to use the Guava Splitter.omitEmptyStrings() method in Java, which is a simple and efficient way to split strings and omit empty strings from the results. We have also explained its benefits and drawbacks, and compared it with some other alternatives that you can use to split strings and handle empty strings in Java.
If you want to learn more about this method, you can check out the Guava official website or its GitHub repository.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)