Java Program to Split a String into Equal Length Substrings
Below are the two examples to implement splitting a string into equal length substrings in Java.
Example 1: Splitting “Hello, World!” into Substrings of Length 5
// 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 length = 5;
// Original string
String input = "Hello, World!";
// Loop through the string by the substring length
for (int i = 0; i < input.length(); i += length)
{
// Extract the substring from the current index to
// the next substring length
String sub = input.substring(
i, Math.min(i + length, input.length()));
System.out.println(sub);
}
}
}
Output
Hello , Wor ld!
Complexity of the above method:
Time Complexity: O(n), here n is the length of the original string.
Auxiliary Space: O(n)
Example 2: Splitting “w3wiki” into Substrings of Length 4
import java.io.*;
public class GFG {
public static void main(String args[])
{
// Define the substring length
int length = 4;
// Original string
String input = "w3wiki";
// Loop through the string by the substring length
for (int i = 0; i < input.length(); i += length) {
// Extract the substring from the current index to
// the next substring length
String sub = input.substring(
i, Math.min(i + length, input.length()));
System.out.println(sub);
}
}
}
Output
Geek sfor Geek s
Complexity of the above method:
Time Complexity: O(n), here n is the length of the original string.
Auxiliary Space: O(n)
Example 3: Using Custom Method
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.
public class Main {
public static void main(String[] args) {
// Define the substring length
int length = 5;
// Original string
String input = "Hello, World!";
// Split the string into substrings of equal length
String[] substrings = splitString(input, length);
// Output the substrings
for (String sub : substrings) {
System.out.println(sub);
}
}
// Custom method to split a string into substrings of equal length
public static String[] splitString(String input, int length) {
int numOfSubstrings = (int) Math.ceil((double) input.length() / length);
String[] substrings = new String[numOfSubstrings];
for (int i = 0; i < numOfSubstrings; i++) {
int start = i * length;
int end = Math.min(start + length, input.length());
substrings[i] = input.substring(start, end);
}
return substrings;
}
}
Output
Hello , Wor ld!
Complexity of the above method:
Time Complexity: O(n), where n is the length of the original string.
Auxiliary Space: O(n)
How to Split a String into Equal Length Substrings in Java?
It is common practice in Java to split strings into smaller strings of equal length to process larger strings into usable substrings. 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 to Split String into Equal Length Substrings
Input: “Hello, World!”, length = 5
Output: “Hello”, “, Wor”, “ld!”Input: “w3wiki”, length = 4
Output: “Geek”, “sfor”, “Geek”, “s”
Method to Split a String into equal-length Substrings in Java
In Java, to split a string into substrings of equal length, we can use the substring method in a loop,
- First, we need to determine the desired length of substring.
- Then, remove the substring of the specified length and reconnect the original string.
- At last, use the substring() method to get a substring from the current index to the next index (based on the length) at each iteration.