Java String substring() Method
Last Updated : 11 Apr, 2025
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); } }
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
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); } }
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.
Similar Reads
Java String trim() Method The trim() method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is "\u0020". It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves
4 min read
Java String split() Method The String split() method in Java is used to split a string into an array of substrings based on matches of the given regular expression or specified delimiter. This method returns a string array containing the required substring.Example: Here, we will split a String having multiple delimiters or re
6 min read
StringJoiner toString() method in Java The toString() of StringJoiner is used to convert StringJoiner to String. It returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are
2 min read
StringBuffer substring() method in Java with Examples In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The strin
4 min read
Java String subSequence() Method with Examples In Java, the String class provides the subSequence() method. This subSequence() method extracts a portion of a string as a CharSequence. This method is useful for retrieving specific character ranges from a string which enhances string manipulation. In this article, we will learn how to use the subS
3 min read
StringJoiner length() method in Java The length() of StringJoiner is used to find out the length of the StringJoiner in characters. It returns the length of the String representation of this StringJoiner. Note that if no add methods have been called, then the length of the String representation (either prefix + suffix or emptyValue) wi
2 min read