Extract a Substring Between Two Characters in a String in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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-defined method of the String class, and it takes the two parameters are starting index and ending index of the string can extract the substring. Step-by-step ImplementationCreate the main method in the Java program.Define the one static with a string value.Take the startChar and endChar as characters after that find the index of the characters with the indexOf() method.Give the startIndex and endIndex, the parameters of the substring method then it finds the substring of the specific between of two characters.Print the substring.Program to Extract a Specific Substring between Two Characters in a String in Java Java // Java program to extract a specific substring between two characters in a String import java.lang.String; public class GFGSubStringExample { //main method public static void main(String[] args) { String inputString = "Welcome to the Geeks for Geeks Tutorial"; // specify the start and end characters char startChar = 'h'; char endChar = 't'; // to find the index of the start character int startIndex = inputString.indexOf(startChar); // to find the the index of the end character after the start character int endIndex = inputString.indexOf(endChar, startIndex + 1); // check if both start and end characters are found if (startIndex != -1 && endIndex != -1) { // extract the substring between the start and end characters String result = inputString.substring(startIndex + 1, endIndex); // print the result System.out.println("Substring between '" + startChar + "' and '" + endChar + "': " + result); } else { System.out.println("Not found"); } } } OutputSubstring between 'h' and 't': e Geeks for Geeks Tu Explanation of the Program:The above the program is the example of the extract the specific substring between two characters in a string. It is easy to implement in the Java program to obtain the starting and ending characters.And it has the index values using the substring method to extract the substring between the characters. Create Quiz Comment S seepanarajvskq Follow 0 Improve S seepanarajvskq Follow 0 Improve Article Tags : Java Java Programs Java-Strings Java Examples Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like