What does Guava’s Splitter class do?
A) It splits strings into components based on a separator
B) It divides files into multiple parts
C) It distributes tasks across multiple threads
D) It breaks down large datasets into smaller subsets
Answer:
A) It splits strings into components based on a separator
Explanation:
Guava’s Splitter
class is used to split strings into components based on a specified separator. It provides a flexible and convenient way to break down strings into parts, handling cases such as trimming whitespace, omitting empty strings, and more.
For example:
Splitter splitter = Splitter.on(", ").omitEmptyStrings().trimResults();
List<String> parts = splitter.splitToList("one, two, , three");
This code splits the string “one, two, , three” into a list of parts, omitting empty strings and trimming whitespace, resulting in the list [“one”, “two”, “three”]. The Splitter
class is the complement of Joiner
and provides powerful string manipulation capabilities.