How to Get Substring Items Within Arraylist in Java? Last Updated : 02 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 will be discussing how to get substring items within ArrayList in Java. Methods to get the substrings items within ArrayList Two methods are mostly used to implement to get the substrings items within ArrayList in the Java program. add(): It is a pre-defined method of the ArrayList that can be used to add the elements to the ArrayList.substring(startIndex, endIndex): It is also an in-built method, and it can be used to generate the substring with the specified starting index and ending index parameter of the method.Java Program to Get the Substrings Items within ArrayList Java // Java Program to Get Substrings from an ArrayList of Strings import java.util.ArrayList; public class GFGSubstringArrayList { // Main method public static void main(String[] args) { // Create the ArrayList of Strings named as mainList ArrayList<String> mainList = new ArrayList<>(); // Adding values to mainList mainList.add("Programming"); mainList.add("Java"); mainList.add("Spring"); mainList.add("SpringBoot"); // Define the startIndex and endIndex of the substring int startIndex = 3; int endIndex = 5; // Get substrings from the ArrayList ArrayList<String> substringList = getSubstrings(mainList, startIndex, endIndex); // Print the result System.out.println("Original ArrayList: " + mainList); System.out.println("Substrings ArrayList: " + substringList); } // Method to get substrings from an ArrayList of Strings private static ArrayList<String> getSubstrings(ArrayList<String> list, int startIndex, int endIndex) { ArrayList<String> substringList = new ArrayList<>(); for (String str : list) { // Check if the string is long enough for the specified indices if (str.length() >= endIndex) { substringList.add(str.substring(startIndex, endIndex)); } else { // Handle the case where the string is shorter than the specified indices substringList.add("No SubString"); } } return substringList; } } OutputOriginal ArrayList: [Programming, Java, Spring, SpringBoot] Substrings ArrayList: [gr, No SubString, in, in] Explanation:In the above program, we are getting the substring item within the ArrayList. We have created the ArrayList and added the elements into the ArrayList.After that defined one more method to convert the substrings with the specified starting index and ending of the parameters of the substring in the program. Comment More infoAdvertise with us Next Article How to Get Substring Items Within Arraylist in Java? K kadambalamatclo Follow Improve Article Tags : Java Java Programs Java-ArrayList substring Practice Tags : Java Similar Reads How to Convert a String to ArrayList in Java? Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet 1 min read How to Print List Items Without Brackets in Java? In Java, if you want to print the elements of a list without brackets [ ]. you need to manually iterate through the list and print each element separately. The brackets are automatically added when you directly print the entire list. In this article, we will discuss how to print list items without b 2 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 How to Convert a Comma-Separated String into an ArrayList in Java? In Java, to convert a comma-separated string into ArrayList, we can use the split() method to break the string into an array based on the comma delimiter. Another method we can also use Java Streams for a functional approach to achieve the same result.Example:Input: "Hello,from,geeks,for,geeks"Outpu 2 min read Removing Element from the Specified Index in Java ArrayList The remove(int index) method present in java.util.ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices). Syntax : public removed_element remove(int index) Parameters: The index of the element 3 min read Like