How to Split a String into Equal Length Substrings in Java? Last Updated : 29 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } } OutputHello , 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 Substrings1. Split a String into Substrings of Length 4 Java 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); } } } OutputGeek 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 StringWe 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. Java 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; } } OutputGeeks forGe eks Explanation: In the above example, the splitString() method divides the string into substrings of equal length. The Comment More infoAdvertise with us Next Article How to Split a String into Equal Length Substrings in Java? susobhanakhuli Follow Improve Article Tags : Java Java Programs Java-Strings Java-String-Programs Practice Tags : JavaJava-Strings Similar Reads Split a String into a Number of Substrings in Java Given a String, the task it to split the String into a number of substrings. A String in java can be of 0 or more characters. Examples : (a) "" is a String in java with 0 character (b) "d" is a String in java with 1 character (c) "This is a sentence." is a string with 19 characters. Substring: A Str 5 min read How to Replace the Last Occurrence of a Substring in a String in Java? In this article, we will learn about replacing the last instance of a certain substring inside a string as a typical need. We'll look at a practical Java solution for this in this post. Replace the last occurrence of a Substring in a String in JavaWe may use the lastIndexOf() method to determine the 2 min read Java program to print Even length words in a String Given a string s, write a Java program to print all words with even length in the given string. Example to print even length words in a String Input: s = "i am Geeks for Geeks and a Geek" Output: am GeekExample:Java// Java program to print // even length words in a string class GfG { public static v 3 min read How to Split a String in Java with Delimiter? In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter. The best example of this is CSV (Comma Separated Values) files.Program to Split a String with Delimiter in JavaHere is a simple program to split a string with a comma 2 min read Java Program To Check If A String Is Substring Of Another Write a java program for a given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples : Input: s1 = "for", s2 = "geeksforgeeks"Output: 5Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 3 min read How to Split Strings Using Regular Expressions in Java? Split a String while passing a regular expression (Regex) in the argument and a single String will split based on (Regex), as a result, we can store the string on the Array of strings. In this article, we will learn how to split the string based on the given regular expression. Example of Split Stri 2 min read Extract a Substring Between Two Characters in a String in Java In Java, the extraction of the substring between the two characters in a string can be implemented with the substring method, and string is a pre-defined method in the class of the String. A String is a pre-defined class of the java.lang package. substring(startIndex, endIndex): This is a pre-define 2 min read How to Extract a Specific Line from a Multi-Line String in Java? In Java, String Plays an important role. As String stores or we can say it is a collection of characters. We want to get a specific line from a multi-line String in Java. This can also be done in Java. In this article, we will be learning how to extract a specific line from a multi-line String in Ja 2 min read How to Get Substring Items Within Arraylist in Java? In Java, ArrayList is the pre-defined class of the Java collection framework, and it is part of java.util package. ArrayList can be used to add or remove an element dynamically in a Java program. It can be dynamically based on the elements added or removed from the ArrayList. In this article, we wil 2 min read Like