Open In App

Java String substring() Method

Last Updated : 11 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the substring() method of the 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 takes 1 parameter or 2 parameters, i.e., start and end value as arguments.
  • In case the end parameter is not specified, then the substring will end at the end of the string.

Syntax of String substring() Method

public String substring(int beginIndex);

public String substring(int beginIndex, int endIndex);

Parameters:

  • beginIndex: Starting index of a substring where we want to specify the starting point of a string, and it is inclusive.
  • endIndex(Optional): The ending index of the substring It is exclusive. If we don’t specify the endIndex, the substring will end at the end of the string.

Return Type:

  • String: It returns a new String object containing which contains the specified substring with the specified index range.

Exceptions:

  • StringIndexOutOfBoundsException: This exception will happen when the index value is in cases such as negative beginIndex or an endIndex is greater than the length of the string or beginIndex is greater than the endIndex.

Note: If we pass the start index and endIndex with the same value so it will return an empty substring (“”).

Example 1: Java program to extract a substring using substring(int beginIndex).

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

public class Geeks {

    public static void main(String[] args)
    {

        String s = "GeeksforGeeks";

        // Extracting substring starting from index 8
        String sub = s.substring(8);

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

Output
Substring: Geeks

Explanation: In the above code example, we use the substring(int beginIndex) method and specify the start index which is 8 and it didn’t specified the end index. So it will make substring from end of the given string and we get the substring as “Geeks”.

Diagramatic Representation:

Java substring

Java Substring


Example 2: Java program to extract a substring from specified start index to specified end index using substring(int beginIndex, int endIndex).

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

public class Geeks 
{

    public static void main(String[] args)
    {

        String s = "Welcome to GeeksforGeeks";

        // Extract substring from index 0(inclusive)
        // to 7 (exclusive)
        String sub = s.substring(0, 7);

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

Output
Substring: Welcome

Explanation: In the above example, we use the substring(beginIndex, endIndex) the substring starts at index 0 and ends at index 7, but the character at index 7 is excluded.



Next Article

Similar Reads