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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。