Splitter fixedLength() method | Guava | Java

The method fixedLength(int length) returns a splitter that divides strings into pieces of the given length. For example, Splitter.fixedLength(2).split(“abcde”) returns an iterable containing [“ab”, “cd”, “e”]. The last piece can be smaller than length but will never be empty.

Syntax:

public static Splitter fixedLength(int length)

Parameters: This method takes length as parameter which is the desired length of pieces after splitting. It is a positive integer value.

Return Value: This method returns a splitter, with default settings, that can split into fixed sized pieces.

Exceptions: This method throws IllegalArgumentException if length is zero or negative.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

When Length is 3 : 
Del
hi
Noi
da
Cha
ndi
gar
h


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

Example 2: To show IllegalArgumentException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

When Length is 0 : 
Exception: java.lang.IllegalArgumentException: 
           The length may not be less than 1


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.