Extract a Substring Between Two Characters in a String in Java Last Updated : 09 Feb, 2024 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. Comment More infoAdvertise with us Next Article Extract a Substring Between Two Characters in a String in Java S seepanarajvskq Follow Improve Article Tags : Java Java Programs Java-Strings Java Examples Practice Tags : JavaJava-Strings Similar Reads 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 Remove first and last character of a string in Java Given a string str, the task is to write the Java Program to remove the first and the last character of the string and print the modified string.Examples:Input: str = "GeeksForGeeks"Output: "eeksForGeek"Explanation: The first and last characters of the given string are 'G' and 's' respectively. Afte 4 min read How to find the first and last character of a string in Java Given a string str, the task is to print the first and the last character of the string. Examples: Input: str = "GeeksForGeeks" Output: First: G Last: s Explanation: The first character of the given string is 'G' and the last character of given string is 's'. Input: str = "Java" Output: First: J Las 3 min read 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 3 min read Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided 2 min read 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 Java Program to Replace Multiple Characters in a String In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below: Using String.replace() methodUsing replaceAll() method Using replaceFirst() method Method 1: Using String.replace() method This method returns a new st 3 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 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 Convert a Java String Against a Pattern Regex? Regular expressions, or "regex," are an effective tool in Java programming for checking texts against a given pattern. This post will walk you through the process of utilizing regular expressions to validate a Java text against a pattern. Prerequisites:String in JavaRegular Expressions in JavaConver 2 min read Like