📜  拆分器限制()方法 |番石榴 |Java

📅  最后修改于: 2022-05-13 01:55:17.021000             🧑  作者: Mango

拆分器限制()方法 |番石榴 |Java

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