Split a string into two equal-length substrings in Java
This post will discuss how to split a string into substrings of equal size in Java.
To split a string into substrings of equal size in Java, we can use one of the following methods:
1. Using String.split() method
We can write a regular expression that matches the position where the previous match ended, and use it to split the string into substrings of a given size with String.split() method. For example, we can use the following code to split the string Thequickbrownfoxjumpsoverthelazydog into substrings of size 3: [The, qui, ckb, row, nfo, xju, mps, ove, rth, ela, zyd, og].
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; class Main { public static void main(String[] args) { String str = "Thequickbrownfoxjumpsoverthelazydog"; int size = 3; String[] tokens = str.split("(?<=\\G.{" + size + "})"); // [The, qui, ckb, row, nfo, xju, mps, ove, rth, ela, zyd, og] System.out.println(Arrays.toString(tokens)); } } |
Here, \G require the match to occur only at the end of the previous match. If there was no previous match, it matches the beginning of the string. The enclosing positive lookbehind (?<=text) matches the specified characters from the end of the last match. The split() method will return an array of strings that are separated by this position.
2. Using String.substring() method
Regex is not recommended and should be avoided if possible. We can use a simple loop and the substring() method of the String class to break the string into substrings of the given size. The substring() method takes two parameters: the starting index and the ending index of the substring. For example, the following code to split a string into a list of substrings of size 3. If an array is needed instead of a list, we can convert list into an array.
|
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; class Main { public static void main(String[] args) { String str = "Thequickbrownfoxjumpsoverthelazydog"; int size = 3; List<String> tokens = new ArrayList<>(); for (int start = 0; start < str.length(); start += size) { tokens.add(str.substring(start, Math.min(str.length(), start + size))); } // [The, qui, ckb, row, nfo, xju, mps, ove, rth, ela, zyd, og] System.out.println(tokens); } } |
3. Using Guava’s Splitter Class
Another good alternative is to use the Splitter class from the Guava library to split the string into substrings of a fixed length. It returns an Iterable instead of an array, which we can convert into a list using the Lists.newArrayList() method. It creates a mutable ArrayList instance containing all elements of the Iterable. For example, the following code to split a string into a list of substrings of size 3:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.base.Splitter; import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { String str = "Thequickbrownfoxjumpsoverthelazydog"; int size = 3; List<String> tokens = Lists.newArrayList(Splitter.fixedLength(size).split(str)); // [The, qui, ckb, row, nfo, xju, mps, ove, rth, ela, zyd, og] System.out.println(tokens); } } |
If an array is needed instead of a list, we can call Iterables.toArray() method in place of Lists.newArrayList(). Here is a sample code that demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; class Main { public static void main(String[] args) { String str = "Thequickbrownfoxjumpsoverthelazydog"; int size = 3; String[] tokens = Iterables.toArray(Splitter.fixedLength(size).split(str), String.class); // [The, qui, ckb, row, nfo, xju, mps, ove, rth, ela, zyd, og] System.out.println(Arrays.toString(tokens)); } } |
That’s all about splitting a string into two equal-length substrings 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 :)