方法limit(int limit)返回一個行為與該拆分器等效的拆分器,但在達到限製後停止拆分。該限製定義了迭代器返回的最大項目數,或splitToList(java.lang.CharSequence)返回的列表的最大大小。例如,Splitter.on(’,).limit(3).split(“ a,b,c,d”)返回包含[“a”,“b”,“ c,d”]的可迭代項。
用法:
public Splitter limit(int limit)
參數:此方法將limit作為參數,該參數是要返回的最大項目數。
返回值:此方法返回具有所需配置的拆分器。
範例1:
// Java code to show implementation of
// limit(int limit) 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 = "geeks, . for, .Hey., "
+ "geeks, ., noida, ., classes";
// Initially setting limit as 3
System.out.println("When Limit is 3:");
// Using limit(int limit) method which
// stops splitting after it reaches the limit.
Iterable<String> result = Splitter.on(',')
.limit(3)
.trimResults()
.split(str);
for (String temp:result) {
System.out.println(temp);
}
// Setting limit as 4
System.out.println("\n\nWhen Limit is 4:");
// Using limit(int limit) method which
// stops splitting after it reaches the limit.
Iterable<String> result1 = Splitter.on(',')
.limit(4)
.trimResults()
.split(str);
for (String temp:result1) {
System.out.println(temp);
}
}
}
輸出:
When Limit is 3: geeks . for .Hey., geeks, ., noida, ., classes When Limit is 4: geeks . for .Hey. geeks, ., noida, ., classes
範例2:
// Java code to show implementation of
// limit(int limit) 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 = "Learn$,,Data $ structures"
+ " 123$ to be $ best Coder..";
// Initially setting limit as 2
System.out.println("When Limit is 2:");
// Using limit(int limit) method which
// stops splitting after it reaches the limit.
Iterable<String> result = Splitter.on('$')
.limit(2)
.trimResults()
.split(str);
for (String temp:result) {
System.out.println(temp);
}
// Setting limit as 4
System.out.println("\n\nWhen Limit is 4:");
// Using limit(int limit) method which
// stops splitting after it reaches the limit.
Iterable<String> result1 = Splitter.on('$')
.limit(4)
.trimResults()
.split(str);
for (String temp:result1) {
System.out.println(temp);
}
}
}
輸出:
When Limit is 2: Learn,,Data $ structures 123$ to be $ best Coder.. When Limit is 4: Learn,,Data structures 123 to be $ best Coder..
注意:
- 省略空字符串時,省略的字符串不計算在內。因此,Splitter.on(',).limit(3).omitEmptyStrings().split(“ a ,,, b ,, c,d”)返回包含[“a”,“b”,“ c,d”]的可迭代對象。
- 當請求修剪時,所有條目(包括最後一個條目)都會被修剪。因此,Splitter.on(',).limit(3).trimResults().split(“ a,b,c,d”)的結果為[“a”,“b”,“ c,d”]。
相關用法
- Java Guava Splitter trimResults()用法及代碼示例
- Java Guava Splitter splitToList()用法及代碼示例
- Java Guava Splitter fixedLength()用法及代碼示例
- Java Guava Splitter omitEmptyStrings()用法及代碼示例
- Java stream.limit()用法及代碼示例
- Java FloatBuffer limit()用法及代碼示例
- Java LongBuffer limit()用法及代碼示例
- Java IntBuffer limit()用法及代碼示例
- Java ShortBuffer limit()用法及代碼示例
- Java Guava IntMath gcd(int a, int b)用法及代碼示例
- Java Guava Chars.contains()用法及代碼示例
- Java Guava Floats.contains()用法及代碼示例
- Java Guava IntMath mean()用法及代碼示例
注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Splitter limit() method | Guava | Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。