How to Split a String into Equal Length Substrings in Java?
In Java, splitting a string into smaller substrings of equal length is useful for processing large strings in manageable pieces. We can do this with the substring method of the loop. This method extracts a substring of the specified length from the input string and stores it in a list.
Example:
In the below example, we will use a for loop and the substring()
method to split a string into substrings of equal length.
// Java program to Split a String into
// Equal Length Substrings
import java.io.*;
public class GFG {
public static void main(String args[]) {
// Define the substring length
int l = 5;
// Original string
String s = "Hello, Geeks";
// Loop through the string
// by the substring length
for (int i = 0; i < s.length(); i += l)
{
// Extract the substring from the
// current index to the next substring length
String sub = s.substring(
i, Math.min(i + l, s.length()));
System.out.println(sub);
}
}
}
// Java program to Split a String into
// Equal Length Substrings
import java.io.*;
public class GFG {
public static void main(String args[]) {
// Define the substring length
int l = 5;
// Original string
String s = "Hello, Geeks";
// Loop through the string
// by the substring length
for (int i = 0; i < s.length(); i += l)
{
// Extract the substring from the
// current index to the next substring length
String sub = s.substring(
i, Math.min(i + l, s.length()));
System.out.println(sub);
}
}
}
Output
Hello , Gee ks
Explanation: In the above example, the string "Hello, Geeks" is split into substrings of length 5. Then the substring()
method is used inside the loop to extract substrings. The Math.min()
method ensures that the substring does not exceed the original string's length.
Java Programs to Split a String into Equal Length Substrings
1. Split a String into Substrings of Length 4
import java.io.*;
public class GFG {
public static void main(String args[]) {
// Define the substring length
int l = 4;
String s = "GeeksforGeeks";
// Loop through the string
// by the substring length
for (int i = 0; i < s.length(); i += l) {
// Extract the substring from the current index
// to the next substring length
String sub = s.substring(
i, Math.min(i + l, s.length()));
System.out.println(sub);
}
}
}
import java.io.*;
public class GFG {
public static void main(String args[]) {
// Define the substring length
int l = 4;
String s = "GeeksforGeeks";
// Loop through the string
// by the substring length
for (int i = 0; i < s.length(); i += l) {
// Extract the substring from the current index
// to the next substring length
String sub = s.substring(
i, Math.min(i + l, s.length()));
System.out.println(sub);
}
}
}
Output
Geek sfor Geek s
Explanation: In the above example, the string "GeeksforGeeks" is split into substrings of length 4.
2. Using a Custom Method to Split the String
We can create a custom method to split a string into substrings of equal length without relying on external libraries. This method involves iterating through the original string and extracting substrings of the desired length. This is a more structured approach.
public class GFG {
public static void main(String[] args) {
// Define the substring length
int l = 5;
// Original string
String s1 = "GeeksforGeeks";
// Split the string into
// substrings of equal length
String[] s2 = splitString(s1, l);
for (String sub : s2) {
System.out.println(sub);
}
}
// Custom method to split a string
// into substrings of equal length
public static String[] splitString(String s1, int l) {
int numOfSubstrings = (int) Math.ceil((double) s1.length() / l);
String[] s2 = new String[numOfSubstrings];
for (int i = 0; i < numOfSubstrings; i++) {
int start = i * l;
int end = Math.min(start + l, s1.length());
s2[i] = s1.substring(start, end);
}
return s2;
}
}
public class GFG {
public static void main(String[] args) {
// Define the substring length
int l = 5;
// Original string
String s1 = "GeeksforGeeks";
// Split the string into
// substrings of equal length
String[] s2 = splitString(s1, l);
for (String sub : s2) {
System.out.println(sub);
}
}
// Custom method to split a string
// into substrings of equal length
public static String[] splitString(String s1, int l) {
int numOfSubstrings = (int) Math.ceil((double) s1.length() / l);
String[] s2 = new String[numOfSubstrings];
for (int i = 0; i < numOfSubstrings; i++) {
int start = i * l;
int end = Math.min(start + l, s1.length());
s2[i] = s1.substring(start, end);
}
return s2;
}
}
Output
Geeks forGe eks
Explanation: In the above example, the splitString()
method divides the string into substrings of equal length. The