Substring in JavaLast Updated : 10 Jan 2026 A substring is a part of a string, or in other words, a subset of another string. In Java, extracting substrings is a common operation used for text processing, data validation, and string manipulation. Java provides built-in methods that make it easy to extract substrings using index values or delimiters. For example, if the string is "computer", possible substrings include "com", "compu", "ter", and more. Note: String index positions start from 0.You can find a substring from a given string in Java using the following methods:
Finding Substring Using substring() MethodThe substring() method of the String class is used to extract a part of a string based on index positions. In this method:
SyntaxIt has the following syntax: This method returns a new string starting from the specified startIndex to the end of the string. It throws an IndexOutOfBoundsException if startIndex is less than 0 or greater than the string length. This method returns a new string starting from startIndex and ending at endIndex. It throws an IndexOutOfBoundsException if startIndex is less than 0, greater than endIndex, or if endIndex exceeds the string length. Understanding startIndex and endIndexLet's understand the startIndex and endIndex by the code given below. Here, 0 points to the first character and 2 points to the character e. Since the end index is exclusive, only characters at index 0 and 1 are included. ExampleThe following example demonstrates finding substrings using substring() method. Output: Original String: SachinTendulkar Substring starting from index 6: Tendulkar Substring starting from index 0 to 6: Sachin The above program demonstrates both variants of the substring() method, where the start index is inclusive and the end index is exclusive. Finding Substring Using String.split() MethodThe split() method of the String class can also be used to extract substrings by dividing a string based on a delimiter or regular expression. It returns an array of strings. SyntaxHere is the syntax to use String.split() method for finding substring: This method splits the given string around matches of the specified regular expression and returns an array of substrings. This overloaded version splits the string based on the given regular expression and limits the number of resulting substrings. ExampleThe following example demonstrates finding substring using the split() method. Output: [Hello, My name is Sachin] In this example, the split() method separates the string based on the comma delimiter and stores the resulting substrings in an array. Finding Substring Using String.indexOf() along with substring()This approach can also be used to find the position of a specific character or word in a string using the indexOf() method and then extracts the required part using the substring() method. It is useful when you know what text you want to start or end the substring with, rather than fixed index values. SyntaxIt has the following syntax: ExampleThe following example demonstrates finding substring using String.indexOf() along with substring() method. Output: Programming Language In this example, indexOf() locates the starting position of the word "Programming", and substring() extracts the string from that position to the end. Next TopicJava-string-methods |
We request you to subscribe our newsletter for upcoming updates.