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


Java Guava Splitter limit()用法及代码示例


方法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”]。


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Splitter limit() method | Guava | Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。