方法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
相關用法
- Java Guava Splitter limit()用法及代碼示例
- Java Guava Splitter splitToList()用法及代碼示例
- Java Guava Splitter omitEmptyStrings()用法及代碼示例
- Java Guava Splitter fixedLength()用法及代碼示例
- Java Guava Floats.max()用法及代碼示例
- Java Guava Doubles.min()用法及代碼示例
- Java Guava Shorts.max()用法及代碼示例
- Java Guava Chars.min()用法及代碼示例
- Java Guava Doubles.max()用法及代碼示例
- Java Guava Chars.max()用法及代碼示例
- Java Guava Longs.max()用法及代碼示例
- Java Guava Floats.min()用法及代碼示例
- Java Guava Shorts.contains()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Splitter trimResults() method | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。