當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。