The Wayback Machine - https://web.archive.org/web/20250102085251/https://www.geeksforgeeks.org/substring-in-java/
Open In App

Java String substring() Method

Last Updated : 09 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Java, substring() method of String class returns a substring from the given string. This method is most useful when you deal with text manipulation, parsing, or data extraction.

  • This method either take 1 parameter or 2 parameters i.e. start and end value as arguments.
  • In case, if the end parameter is not specified then the substring will end at the end of the string.

Syntax:

public String substring(int begIndex);

or

public String substring(int begIndex, int endIndex)

Example 1: Here, we are using the substring(int begIndex) method to extract a substring from the given string starting at index 6 and ending at the end of the string. The substring begins at index 7, and since no endIndex is provided, it continues to the end of the string. begIndex is inclusive.

Java
// Java program to show the use of
// substring(int begIndex)

public class Geeks {

    public static void main(String[] args)
    {

        String str = "GeeksforGeeks";

        // Extracting substring starting from index 7
        String substr = str.substring(7);

        // printing the substring
        System.out.println("Substring: " + substr);
    }
}

Output
Substring: rGeeks



Java substring

Java Substring


Example 2: Here, we will use the String substring(begIndex, endIndex) method. This method will return the substring that begins with the character at the specified index and ends at endIndex – 1

  • begIndex – the begin index, inclusive.
  • endIndex – the end index, exclusive.
Java
// Java program to show the use of
// substring(int begIndex, int endIndex)

public class Geeks {

    public static void main(String[] args)
    {

        String str = "Welcome to GeeksforGeeks";

        // Extract substring from index 0(inclusive)
        // to 12 (exclusive)
        String substr = str.substring(0, 12);

        // printing the substring
        System.out.println("Substring: " + substr);
    }
}

Output
Substring: Welcome to G

Here, the substring starts at index 0 and ends at index 12, but the character at index 12 is excluded.

Working with substrings is a common requirement in Java. To get hands-on experience with different string operations, including substring manipulation, the Java Programming Course offers in-depth examples and practical exercises.

Important Points:

  • Inclusive and Exclusive Indices: The substring() method includes the starting index (inclusive) but excludes the ending index (exclusive). This is an important distinction when specifying the indices.
  • Index Out of Bounds Exception: If you provide an invalid index (negative or greater than the string’s length), the method will throw a StringIndexOutOfBoundsException. Ensure your indices are valid.
  • Empty Substring: If startIndex equals endIndex, the substring() method will return an empty string.


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg