Splitter trimResults() method | Guava | Java

The method trimResults() returns a splitter that behaves equivalently to this splitter, but automatically removes leading and trailing whitespace from each returned substring. For example,
Splitter.on(‘, ‘).trimResults().split(” a, b, c “) returns an iterable containing [“a”, “b”, “c”].

Syntax:

public Splitter trimResults()

Return Value: This method returns a splitter with the desired configuration.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// trimResults() 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,  geeks, for,  geeks,  noida";
  
        // Using trimResults() method. Strings that
        // have been split apart often need to be
        // trimmed. They often have surrounding whitespace.
        // With trimResults(), Splitter does this.
        List<String> myList = Splitter.on(',')
          .trimResults().splitToList(str);
  
        for (String temp : myList) {
            System.out.println(temp);
        }
    }
}

chevron_right


Output:



Hello
geeks
for
geeks
noida

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// trimResults() 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 = "Everyone. .  should. Learn. Data. Structures";
  
        // Using trimResults() method. Strings that
        // have been split apart often need to be
        // trimmed. They often have surrounding whitespace.
        // With trimResults(), Splitter does this.
        List<String> myList = Splitter.on('.')
            .trimResults().splitToList(str);
  
        for (String temp : myList) {
            System.out.println(temp);
        }
    }
}

chevron_right


Output:

Everyone

should
Learn
Data
Structures

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.