Thursday, September 18, 2025
HomeLanguagesJavaSplitter trimResults() method | Guava | Java

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:




// 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);
        }
    }
}


Output:

Hello
geeks
for
geeks
noida

Example 2:




// 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);
        }
    }
}


Output:

Everyone

should
Learn
Data
Structures
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11835 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7053 POSTS0 COMMENTS
Thapelo Manthata
6736 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS