While working with text or string data, there may be scenarios where users need to extract a specific substring from a string. This could involve extracting a single word from a sentence, retrieving a couple of letters from a word, and so on. To deal with such situations, Java offers a built-in “substring()” method that fetches the substrings from a specific string.
Substring is a programming term that is defined as a piece of text that is extracted from a larger string/text which is also known as the strings subset. For example the substrings of the string “Hello” will be “Hel”, “Hell”, “elo”, etc.
In this article, we will implement the substring() method of Java through code examples.
How to Implement Java substring() Method?
The substring is a subset of the specified string. The string class is implemented through the substring() method of Java. There are two implementations of the substring() method, as discussed below:
- public String substring(int startIndex): This method returns the string from the specified starting index of the string.
- public String substring(int startingIndex, int endingIndex): This method takes two parameters which are the starting index and ending index. The substring is returned from the specified starting to ending index.
Example 1: Implementation of substring () Method in Java
The substring() method is implemented below to get the specified substrings from the entered string.
//Declare class for substring
class SUBSTRING {
//Declare Main Class
public static void main(String[] args) {
System.out.println("FIRST EXAMPLE");
//Enter the string to obtain its substring
String s1 = "JavaWorld";
//Enter the beginning and ending index value
System.out.println(s1.substring(0, 4));
System.out.println(s1.substring(0, 9));
System.out.println(s1.substring(1, 7));
//Another Example to show substring
System.out.println("SECOND EXAMPLE");
//Enter the string to get its substring
String s2 = "Programming";
//Print the results by entering the starting and ending index.
System.out.println(s2.substring(0, 4));
System.out.println(s2.substring(0, 9));
}
}
In the above code:
- A class is declared for the substring method.
- To get the substring, a string is declared as s1 in the Main class.
- The value of the starting index value and the value of the ending index are retrieved using the substring() method.
- The output is printed accordingly.
Output
The output below depicts that in the FIRST EXAMPLE the substrings of the string “JavaWorld” are printed. In the SECOND EXAMPLE the substrings for the string “Programming” are printed as the output.
Example 2: Implementation of substring() Method Using for Loop
The below code is implemented using the for loop to find the substring from the given string.
public class substrings
{
//The Main method of Java
public static void main(String argvs[])
{
//Declare the string to get the substring
String str[] =
{
"Cyber Security", "Systems Security", "Database Management",
"Information Security",
"Operating System Design"
};
//The term "Security" is to be fetch
String lastname = "Security";
int lastnameSize = lastname.length();
int size = str.length;
for(int x = 0; x < size; x++)
{
//The substring() method to get the last name that is "Security".
int len = str[x].length();
String subStr = str[x].substring(len - lastnameSize);
if(subStr.equals(lastname))
{
//The items with the "Security" output is to be printed
System.out.println(str[x]);
}
}
}
}
In the above code:
- The strings are declared in the Main class of Java.
- The strings that contain “Security” as a suffix are printed.
- The length of the string declared as lastname and the length of the string declared in the Main class are compared.
- When the length matches, the equals() method compares the two strings and outputs the result using the println() method.
Output
The output below implements the substring method to get the strings that contain “Security”.

This sums up the implementation of the substring() method in Java
Conclusion
The substring() method is a subset or a part of a string that is implemented through the String Class in Java. It is a built-in method in Java that is used to extract a substring from a described string using specified index values. This article has effectively demonstrated the working of the substring() method in Java.