What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Substring in Java (with examples)

  • Nov 05, 2023
  • 10 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Nihal Mahansaria
Substring in Java (with examples)

In Java, the substring is a powerful method that allows developers to extract subsets or parts of a string. By using the substring() method, you can create smaller strings from a bigger one without modifying the original string. In this article, we will explore the different variants of the substring() method, understand its syntax, and learn how to use it effectively in Java programming.

What are Substrings in Java?

In Java, a substring is a contiguous sequence of characters extracted from a given string. It is defined by specifying the starting and ending indices within the original string. The starting index is inclusive, meaning the character at that position is part of the substring, while the ending index is exclusive, indicating that the character at the ending index is not included in the substring. Substrings are a fundamental tool for manipulating and working with specific sections of strings, making them essential for tasks like data extraction, parsing, and text processing in Java. 

Importance of Substrings in String Manipulation

Substrings are essential for several reasons:

  • Extracting relevant information from a larger text.
  • Parsing data, such as dates or URLs.
  • Tokenization and text analysis in natural language processing.
  • Creating a new string from a part of the original string.

Understanding and effectively using substrings are valuable skills for Java developers.

Syntax of the substring() Method

The String class in Java provides the substring() method, which allows you to create substrings from a given string. Let's dive into the syntax of this method.

Syntax of the substring() Method:

String substring(int beginIndex)

String substring(int beginIndex, int endIndex)



  • beginIndex: The index at which the substring starts (inclusive).
  • endIndex: The index at which the substring ends (exclusive).

The substring() method returns a new string that is a substring of the original string, starting from beginIndex and ending before endIndex.

How to Create Substrings?

Now that we understand the syntax, let's explore how to create substrings from a given string.

How to use substring() Method?

To create a substring, you can call the substring() method on a string and provide the appropriate indices:

String originalString = "Favtutor";
String substring = originalString.substring(2, 5);


In this example, we start the substring at index 2 (inclusive) and end it at index 5 (exclusive). The resulting substring will be "vtu"


public class SubstringExample {
    public static void main(String[] args) {
        String originalString = "Favtutor";
        String substring = originalString.substring(2,5);
        System.out.println("Substring: " + substring);



Substring: vtu


The substring() method extracts the characters between the specified indices and returns a new string.

What are Substring Indices and Ranges?

When working with substrings, it's essential to understand how to specify the starting and ending points for substring extraction.

How to use Indices and Ranges to Define Substrings?

Indices are used to specify the exact positions where the substring starts and ends.

String text = "Favtutor";
String substringByIndices = text.substring(0,4); // "Favt"


In this example, we specify the exact indices for substring extraction.

Ranges allow you to define a substring based on a beginning and ending point, which can be more flexible.

String text = "Java Programming";
String substringByRange = text.substring(5, text.length() - 3); // "Program"


Here, we use a range to create a substring, starting at index 5 and ending three characters before the end ef the string.

Examples of Substring Operations 

Let's delve into practical examples of substring operations in Java.

Example 1: Extracting a File Extension

public class FileExtensionExample {
    public static void main(String[] args) {
        String fileName = "document.pdf";
        String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
        System.out.println("File Extension: " + fileExtension);



File Extension: pdf


In this example, we extract the file extension from a given file name. The lastIndexOf() method is used to find the position of the last dot, and the substring() method extracts the extension.

Example 2: Parsing a Date

public class DateParsingExample {
    public static void main(String[] args) {
        String date = "2023-11-02";
        String year = date.substring(0, 4);
        String month = date.substring(5, 7);
        String day = date.substring(8, 10);
        System.out.println("Year: " + year);
        System.out.println("Month: " + month);
        System.out.println("Day: " + day);



Year: 2023
Month: 11
Day: 02


In this example, we parse a date string into its components using the substring() method. By specifying the start and end indices, we extract the year, month, and day.

Example 3: Extracting a URL Path

public class URLPathExample {
    public static void main(String[] args) {
        String url = "https://www.example.com/path/to/resource";
        int startIndex = url.indexOf('/') + 2;
        int endIndex = url.lastIndexOf('/') + 1;
        String path = url.substring(startIndex, endIndex);
        System.out.println("Path: " + path);



Path: path/to/resource/


In this example, we demonstrate extracting the path from a URL. We find the positions of the first and last slashes and use the as indices for creating the substring.

Handling Edge Cases

When working with substrings, it's crucial to handle edge cases, especially when the specified indices are out of bounds. For example:

public class EdgeCaseExample {
    public static void main(String[] args) {
        String text = "Java";
        try {
            String invalidSubstring = text.substring(2, 5);
            System.out.println("Substring: " + invalidSubstring);
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());



Error: String index out of range: 5


In this case, attempting to create a substring from index 2 to 5 results in a StringIndexOutOfBoundsException because the ending index is out of bounds. Always ensure that your indices are within the valid range to avoid runtime errors.

Substring Vs Subsequence

It's essential to clarify the difference between substrings and subsequences in Java.

  • Substring: A substring is a continuous sequence of characters extracted from a string. It consists of characters in contiguous positions within the original string.
  • Subsequence: A subsequence is a sequence of characters that may not be contiguous within the original string. It can contain characters that are not adjacent in the original string.

The choice between substrings and subsequences depends on specific requirements. If you need a continuous sequence, use substrings. If you need a sequence with gaps, use subsequences.

Performance Considerations while Creating Substrings

When working with substrings, be mindful of performance considerations. Substrings in Java are backed by the original string, which means they share the underlying character array. This can lead to memory issues if you retain large substrings. To avoid excessive memory consumption, consider creating new strings from substrings when needed.

Best Practices for Substring Manipulation

To use substrings effectively, follow these best practices:

  • Ensure indices are within the valid range to avoid exceptions.
  • Be cautious when retaining large substrings to prevent memory issues.
  • When dealing with performance-critical applications, consider alternatives to substring manipulation, such as using character arrays.


In conclusion, substrings are a fundamental concept in Java string manipulation. They allow you to extract specific portions of strings, making them an essential tool for data extraction, parsing, and text processing. Understanding the syntax, indices, ranges, and best practices for substring manipulation is crucial for writing clean and efficient Java code.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Nihal Mahansaria
As a computer science student with a passion for technology and a drive to learn, I have developed a diverse set of skills in web development, machine learning, and technical content writing. With experience in multiple front-end and back-end frameworks, including Flask and Python, I am able to create scalable and efficient web applications.