Guava Splitter.trimResults() method in Java
In this post, we will learn how to use Splitter.trimResults() method from Guava in Java. We will see what the method does, how to use it, and what benefits it offers. We will also provide some examples and code snippets to demonstrate its usage in different scenarios.
1. Overview of Splitter.trimResults() method
The Guava Splitter.trimResults() method is an instance method that returns a new Splitter instance that behaves equivalently to the original one, but automatically removes leading and trailing whitespace from each returned substring. The signature of the method is as follows:
|
1 2 |
public Splitter trimResults() public Splitter trimResults(CharMatcher trimmer) |
The return type of the method is a Splitter that represents the new splitter with the desired configuration. The method can be chained with other methods of the Splitter class, such as on(), omitEmptyStrings(), limit(), etc., to create a customized splitter for your specific needs.
2. Usage of Splitter.trimResults() method
To use the Guava Splitter.trimResults() method, you need to have Guava in your classpath. Then, you can import the Splitter class from com.google.common.base package and create a Splitter instance using the on() method and pass the delimiter as an argument. The delimiter can be a single character, a string, or a regular expression pattern. Next, you can call the trimResults() method on the splitter instance to return a new splitter that will trim each substring. Finally, you can use the split() or splitToList() methods on the splitter instance to split a string into an iterable or a list of substrings. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import com.google.common.base.Splitter; import java.util.List; class Main { public static void main(String[] args) { String str ="a,, b, c,,,d"; // Trim each substring after splitting on comma Splitter splitter = Splitter.on(',').trimResults(); // Returns an iterable of substrings Iterable<String> result1 = splitter.split(str); System.out.println(result1); // [a, , b, c, , , d] // chain with omitEmptyStrings() method to omit empty strings splitter = Splitter.on(',').omitEmptyStrings().trimResults(); // Returns a list of substrings List<String> result2 = splitter.splitToList(str); System.out.println(result2); // [a, b, c, d] } } |
3. Benefits of using Splitter.trimResults() method
There are several benefits of using Guava Splitter.trimResults() method:
- It is concise and expressive. You don’t need to write extra code to trim each substring. You can simply chain the methods of the
Splitterclass to create a fluent and readable expression. - It is consistent and flexible. You can use it with any delimiter, regardless of whether it is a single character, a string, or a regular expression pattern. You can also customize the splitter with other options.
- It is lazy and efficient. You can use it with an iterable instead of an array, which saves memory and allows you to iterate over the substrings without creating them all at once. You can also reuse the same splitter instance for multiple strings.
4. Conclusion
The Splitter.trimResults() method is a handy tool that can help you split strings in Java more easily and elegantly. It can save you time and effort by automatically removing leading and trailing whitespace from each returned substring. It can also make your code more readable and maintainable by using a fluent and flexible syntax.
If you are interested in learning 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 :)