拆分器 trimResults() 方法 |番石榴 |Java
方法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 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 myList = Splitter.on('.')
.trimResults().splitToList(str);
for (String temp : myList) {
System.out.println(temp);
}
}
}
输出:
Everyone
should
Learn
Data
Structures