拆分器 splitToList() 方法 |番石榴 |Java
splitToList(CharSequence sequence)方法将序列拆分为字符串组件,并将它们作为不可变列表返回。
句法:
public List
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 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 myList = Splitter.on('.').splitToList(str);
for (String temp : myList) {
System.out.println(temp);
}
}
}
输出:
Everyone
should
Learn, Data
Structures