Split a String into fixed-length chunks in Java
This post will discuss how to split a string into fixed-length chunks in Java where the last chunk can be smaller than the specified length.
1. Using Guava
If you prefer the Guava library, you can use the Splitter
class. For example, the expression Splitter.fixedLength(n).split(s)
returns a splitter that divides the string s
into chunks of length n
.
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.base.Splitter; public class Main { public static void main(String[] args) { String s = "ABCDEFGHIJKLMNO"; int chunkSize = 5; Iterable<String> chunks = Splitter.fixedLength(chunkSize).split(s); System.out.println(chunks); // [ABCDE, FGHIJ, KLMNO] } } |
2. Using String.split()
method
The String class contains the split()
method that splits a string around matches of the given regular expression. You can use it to split a string into equal size chinks. The following code uses the split()
method to split a string into equal size chunks of length 5 using the regular expression (?<=\\G.{5})
that uses lookbehind.
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; public class Main { public static void main(String[] args) { String s = "ABCDEFGHIJKLMNO"; int chunkSize = 5; String[] chunks = s.split("(?<=\\G.{" + chunkSize + "})"); System.out.println(Arrays.toString(chunks)); // [ABCDE, FGHIJ, KLMNO] } } |
3. Using String.substring()
method
The idea is to extract each chunk using the String.substring(int, int)
method to partition the string into fixed-length slices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.List; public class Main { public static List<String> split(String s, int chunkSize) { List<String> chunks = new ArrayList<>(); for (int i = 0; i < s.length(); i += chunkSize) { chunks.add(s.substring(i, Math.min(s.length(), i + chunkSize))); } return chunks; } public static void main(String[] args) { String s = "ABCDEFGHIJKLMNO"; int chunkSize = 4; System.out.println(split(s, chunkSize)); // [ABCD, EFGH, IJKL, MNO] } } |
If you use Java 8 or above, you can use Stream API. Here's the equivalent code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static List<String> split(String s, int chunkSize) { return IntStream.iterate(0, i -> i < s.length(), i -> i + chunkSize) .mapToObj(i -> s.substring(i, Math.min(s.length(), i + chunkSize))) .collect(Collectors.toList()); } public static void main(String[] args) { String s = "ABCDEFGHIJKLMNO"; int chunkSize = 4; System.out.println(split(s, chunkSize)); // [ABCD, EFGH, IJKL, MNO] } } |
4. Using Pattern.compile()
method
To split a string into fixed-length chunks, you can create a matcher to determine the indexes of the chunk. Here's how the code would look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String s = "ABCDEFGHIJKLMNO"; int chunkSize = 5; Matcher match = Pattern.compile(".{1," + chunkSize + "}").matcher(s); List<String> chunks = new ArrayList<>(); while (match.find()) { chunks.add(s.substring(match.start(), match.end())); } System.out.println(chunks); // [ABCDE, FGHIJ, KLMNO] } } |
That's all about splitting a string into fixed-length chunks in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)