Substring in Java
Updated on 19th Dec, 23 9.1K Views

In this blog, you will learn everything about substring in Java, from what it is to how to extract a substring using different methods with examples. Moreover, you will also discover how the string() method works internally.

Table of Contents:

Watch this Java Tutorial for Beginners to gain in-depth Knowledge:

What is a Substring?

A substring is an ordered sequence of characters that can be extracted from a string object in Java. In other words, it is a string inside a string. For example, consider that we have the string “Intellipaat”. The substrings of this string will be “Int”, “telli” “Intelli”, “paat”, etc. Java provides in-built methods for extracting a substring from a string. Because of the immutability property of the string data type in Java, any substring we extract from an existing String object must be stored in a new String variable.

Go for this in-depth job-oriented Java Training Course now!

How to Extract Substrings from Strings

How to Extract Substrings from Strings

As mentioned in the above section, Java has its own built-in methods to extract a substring. These methods are part of the Java String class. In the following sections, we will be discussing these methods in detail.

Using substring(begIndex) method

The substring(begIndex) method in Java returns a substring of the original string, starting from the specified index and extending to the end of the original string.

The syntax for the substring(begIndex) method is as follows:

String substring(int beginIndex)

The “beginIndex” parameter refers to the index of the first character to be included in the substring, where the index of the first character in the string is 0. Calling the `substring(begIndex)` method creates a new string object that contains the specified substring. To gain a clearer understanding of this method, take a look at the following code of Hello World in java.

public class SubstringExample {
    public static void main(String[] args) {
        // Original string
        String str = "Hello, World!";
        // Extracting substring starting from index 7
        String substring = str.substring(7); // substring is "World!"
        // Printing the extracted substring
        System.out.println(substring); // prints "World!"
    }
}

Output of this program is:

World!

In this example, the substring (begIndex) method is applied to the “str” string with the parameter 7. This means that the substring starts at index 7, corresponding to the letter ‘W’ in “Hello, World!”. The substring extends to the end of the original string (index 12). The resulting substring is assigned to the “substring” variable and then printed to the console.

Before using this method, you should keep the following points in mind:

  • The “beginIndex” parameter must be a non-negative integer.
  • If the “beginIndex” parameter exceeds the length of the string, an “IndexOutOfBoundsException” will be thrown.
  • The substring includes the character at “beginIndex”.
  • The substring does not include the character at index “beginIndex + 1”.

Get 100% Hike!

Master Most in Demand Skills Now !

Using substring(begIndex, endIndex) method

The substring (int begIndex, int endIndex) method in Java takes two parameters, begIndex and endIndex, indicating the starting and ending indices of the substring, respectively. The substring extracted lies between the character at begIndex and the character at the endIndex – 1. 

The syntax for extracting a substring using this particular method is:

public String substring(int begIndex, int endIndex)

Parameters:

begIndex: The index of the first character to include in the substring.

endIndex: The index of the character after the last character to include in the substring.

It returns a new string object that contains the specified substring.

Consider the same example we mentioned in the previous section, but now with substring (begIndex, endIndex). 

public class SubstringExample {
    public static void main(String[] args) {
        // Original string
        String str = "Hello, World!";
        // Extracting substring from index 7 to 11
        String substring = str.substring(7, 12); // Output: "World"
        // Printing the extracted substring
        System.out.println(substring); // Output: "World"
    }
}

In this example, the substring starts at index 7 (the letter ‘W’ in “Hello, World!”) and extends to index 12 (the character after the letter ‘d’ in “World!”). The resulting substring is assigned to the “substring” variable and then printed to the console.

Using substringAfter() method 

The substringAfter() method is provided by the widely-used Apache Commons Lang library in Java. It allows for the extraction of a substring from a string. This substring begins after the first occurrence of a specified separator string. The syntax for using the substringAfter() method is:

public static String substringAfter(String str, String separator)

The syntax includes two parameters, which are:

str: The string from which the substring is to be extracted.

separator: The separator string marks the beginning of the substring.

This returns a new string object that contains the substring that comes after the first occurrence of the separator. If the separator is not found, an empty string is returned.

Consider the following code to understand this concept:

// Import the StringUtils class from Apache Commons Lang library
import org.apache.commons.lang3.StringUtils;
public class SubstringExample {
    public static void main(String[] args) {
        // Original string
        String str = "Hello, World!";
        // Extracting substring after the first occurrence of ","
        String substring = StringUtils.substringAfter(str, ",");
        // Printing the extracted substring
        System.out.println(substring); // Output: World!
    }
}

This program gives the output given below:

 World!

In this example, the substringAfter() method is invoked on the “str” string with the parameter “,”. This implies that the substring starts after the first occurrence of the “,” character in the string. The resulting substring is assigned to the “substring” variable and then printed to the console.

While using this method, it is important to keep in mind that:

  • The substringAfter() method is case-sensitive.
  • If the separator string is not found in the input string, an empty string is returned.
  • The method returns the substring starting after the first occurrence of the separator, excluding the separator itself.

Apart from these methods, there is another method for extracting substrings in Java, i.e., the built-in .split() method, which is also a part of the Java String class. This method divides a String into one or more substrings separated by a specified delimiting character (such as space, comma, char value, etc.) and returns these substrings in an array.

Although this approach can benefit certain situations, it is generally less well-suited for extracting precise excerpts from existing strings than the more targeted functionality provided by the .substring() method.

Check out this blog on Java fundamentals to enhance your knowledge.

Internal Implementation of substring() method

The internal implementation of the “substring()” method in Java involves creating a new “String” object based on the specified indices. A new string object is created to represent the substring. This object does not possess its own character array; rather, it references the character array of the original string object. For the “substring(begIndex)” method, the implementation checks whether the provided “beginIndex” is within the valid range of the string and then creates a new “String” object using the character array starting from the specified index.

Similarly, for the “substring(begIndex, endIndex)” method, additional checks are performed to ensure both the “beginIndex” and “endIndex” fall within the valid range and that “beginIndex” is less than or equal to “endIndex”. The resulting “String” object represents the substring delimited by the specified indices.

The following program shows the internal workings of the string() method in a comprehensive way:

public class MyString {
    private char[] value;
    public MyString(String str) {
        this.value = str.toCharArray();
    }
    public MyString substring(int startIndex) {
        if (startIndex < 0 || startIndex > value.length) {
            throw new StringIndexOutOfBoundsException(startIndex);
        }
        return ((startIndex == 0) ? this : new MyString(new String(value, startIndex, value.length - startIndex)));
    }
    public MyString substring(int startIndex, int endIndex) {
        if (startIndex < 0 || endIndex > value.length || startIndex > endIndex) {
            throw new StringIndexOutOfBoundsException();
        }
        return ((startIndex == 0 && endIndex == value.length) ? this
                : new MyString(new String(value, startIndex, endIndex - startIndex)));
    }
    // Custom toString method for printing the content
    @Override
    public String toString() {
        return new String(value);
    }
    public static void main(String[] args) {
        // Create a MyString object
        MyString myStr = new MyString("Hello, World!");
        // Demonstrate substring functionality
        MyString subStr1 = myStr.substring(7);
        MyString subStr2 = myStr.substring(7, 12);
        // Print the original string and substrings
        System.out.println("Original String: " + myStr);
        System.out.println("Substring 1: " + subStr1);
        System.out.println("Substring 2: " + subStr2);
    }
}

The output of this program will be:

Original String: Hello, World!

Substring 1: World!

Substring 2: World

Learn Java from scratch with this free Java Tutorial!

Career Transition

Fresher To Web Developer | How To Get Campus Placements Easily | Intellipaat Career Transition
How did I transition from Software Tester to Developer | Got Job in Cognizant | Intellipaat
Intellipaat Review | Got Job with 400% of Hike in 30 Days | Content Writer to SQL Career Transition
How to become a Full Stack Developer | Web Developer to Full Stack Engineer Career Transition
How I did a career change into UI UX Design | Non IT to UI UX Career Transition | Intellipaat
Best UI UX Design Course | Project Management to UI UX Career Transition | Intellipaat

Applications of Substring in Java

Substring operations in Java are fundamental and find applications in various scenarios across software development. Here are some common applications of the substring() method:

  • Data Extraction: Extract specific portions of a string to isolate and analyze relevant data. This is particularly useful in parsing and processing information from larger string inputs.
  • Text Manipulation: Modify or manipulate text by extracting substrings and concatenating them to create new strings. This is often applied to  text processing tasks such as formatting or altering content.
  • Input Validation: Validate user input by checking for specific substrings within the entered text. This ensures that the input complies with predefined patterns or requirements.
  • String Comparison: Compare substrings to determine equality or similarity between different strings. This is valuable in tasks where specific portions of strings need to be matched.
  • Tokenization: Break down a string into tokens by extracting substrings based on delimiters. This is commonly employed in parsing input from files or breaking down sentences into words.
  • URL Manipulation: Extract information from URLs, such as retrieving the domain or path, by working with substrings. This is useful for web development and data analysis tasks.
  • Data Cleaning: Clean and preprocess data by extracting substrings to remove unwanted characters, spaces, or special symbols from strings.
  • String Trimming: Trim unnecessary spaces or characters from the beginning or end of a string by using substring operations. This is often employed in data cleaning and formatting.

Conclusion

The substring() method in Java is an important tool for working with text data. It is widely used for extracting and manipulating portions of strings, making it important for various programming tasks. Whether it’s parsing data, processing user input, handling files, or modifying strings, substring() is a versatile and efficient function. Its widespread adoption in Java applications underscores its significance as a fundamental tool for programmers dealing with text-based data.

To resolve your doubts, visit Intellipaat’s Java Community page.

Course Schedule

Name Date Details
Python Course 24 Feb 2024(Sat-Sun) Weekend Batch
View Details
Python Course 02 Mar 2024(Sat-Sun) Weekend Batch
View Details
Python Course 09 Mar 2024(Sat-Sun) Weekend Batch
View Details

Leave a Reply

Your email address will not be published. Required fields are marked *

Speak to our course Advisor Now !

Related Articles

Associated Courses

Subscribe to our newsletter

Signup for our weekly newsletter to get the latest news, updates and amazing offers delivered directly in your inbox.