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


Java Guava Splitter splitToList()用法及代碼示例


splitToList(CharSequence sequence)方法將序列拆分為字符串成分,並將其作為不可變列表返回。

用法:

public List<String> 
    splitToList(CharSequence sequence)

參數:此方法將序列作為參數,即要分割的字符序列。


返回值:此方法返回從參數分割的分段的不可變列表。

以下示例說明了splitToList()方法的用法方式:

範例1:

// Java code to show implementation of 
// splitToList 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"; 
  
        // SplitToList returns a List of the strings. 
        // This can be transformed to an ArrayList 
        // or used directly in a loop. 
        List<String> myList = Splitter.on(',').splitToList(str); 
  
        for (String temp:myList) { 
            System.out.println(temp); 
        } 
    } 
}
輸出:
Hello
 geeks
 for
 geeks
 noida

範例2:

// Java code to show implementation of 
// splitToList 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"; 
  
        // SplitToList returns a List of the strings. 
        // This can be transformed to an ArrayList 
        // or used directly in a loop. 
        List<String> myList = Splitter.on('.').splitToList(str); 
  
        for (String temp:myList) { 
            System.out.println(temp); 
        } 
    } 
}
輸出:
Everyone
 should
 Learn, Data
 Structures


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Splitter splitToList() method | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。