当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Guava Splitter trimResults()用法及代码示例


方法trimResults()返回一个拆分器,该拆分器的行为与此拆分器相同,但会自动从每个返回的子字符串中删除前导和尾随空格。例如,Splitter.on(',).trimResults().split(“ a,b,c”)返回包含[“a”,“b”,“c”]的可迭代对象。

用法:

public Splitter trimResults()

返回值:此方法返回具有所需配置的拆分器。


范例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); 
        } 
    } 
}
输出:
Hello
geeks
for
geeks
noida

范例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); 
        } 
    } 
}
输出:
Everyone

should
Learn
Data
Structures


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Splitter trimResults() method | Guava | Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。