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


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


fixedLength(int length)方法返回一个分隔符,该分隔符将字符串分成给定长度的片段。例如,Splitter.fixedLength(2).split(“abcde”)返回包含[“ab”,“cd”,“e”]的可迭代。最后一块可以小于长度,但永远不会为空。

用法:

public static Splitter fixedLength(int length)

参数:该方法将长度作为参数,该参数是分割后所需的碎片长度。它是一个正整数。


返回值:此方法返回具有默认设置的拆分器,该拆分器可以拆分为固定大小的片段。

异常:如果length为零或负数,则此方法引发IllegalArgumentException。

范例1:

// Java code to show implementation of 
// fixedLength(int length) 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 = "Delhi Noida Chandigarh"; 
  
        // Initially setting length as 3 
        System.out.println("When Length is 3:"); 
  
        // Using fixedLength(int length) method which 
        // returns a splitter that divides strings 
        // into pieces of the given length 
        Iterable<String> result = Splitter.fixedLength(3) 
                                      .trimResults() 
                                      .split(str); 
  
        for (String temp:result) { 
            System.out.println(temp); 
        } 
  
        // Setting length as 4 
        System.out.println("\n\nWhen Length is 4:"); 
  
        // Using fixedLength(int length) method which 
        // returns a splitter that divides strings 
        // into pieces of the given length 
        Iterable<String> result1 = Splitter.fixedLength(4) 
                                       .trimResults() 
                                       .split(str); 
  
        for (String temp:result1) { 
            System.out.println(temp); 
        } 
    } 
}
输出:
When Length is 3:
Del
hi
Noi
da
Cha
ndi
gar
h


When Length is 4:
Delh
i No
ida
Chan
diga
rh

范例2:显示IllegalArgumentException

// Java code to show implementation of 
// fixedLength(int length) 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) 
    { 
  
        try { 
            // Creating a string variable 
            String str = "GeeksforGeeks is best"; 
  
            // Initially setting length as 0 
            // This should throw "IllegalArgumentException" 
            // as length is 0 
            System.out.println("When Length is 0:"); 
  
            // Using fixedLength(int length) method which 
            // returns a splitter that divides strings 
            // into pieces of the given length 
            Iterable<String> result = Splitter.fixedLength(0) 
                                          .trimResults() 
                                          .split(str); 
  
            for (String temp:result) { 
                System.out.println(temp); 
            } 
        } 
        catch (Exception e) { 
            System.out.println("Exception:" + e); 
        } 
    } 
}
输出:
When Length is 0:
Exception:java.lang.IllegalArgumentException:
           The length may not be less than 1


相关用法


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