The substring() method in Java is used to extract a portion (sub-string) of a string. It belongs to the String class and allows you to create a new string by selecting a part of an existing string.
You can extract a substring using substring( ). It has two forms. The first is String substring(int startIndex).
Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string. The second form of substring( ) allows you to specify both the beginning and ending index of the substring:
Parameters in Method
1. String substring(int beginIndex)
This method returns a new string that starts from the specified beginIndex to the end of the string.
Example:
public class SubstringSingleParameterExample {
public static void main(String[] args) {
String str = "Hello, World!";
// Extract substring from index 7 to the end of the string
String subStr1 = str.substring(7);
System.out.println(subStr1); // Output: World!
}
}
Explanation: substring(7) starts at index 7 and returns the substring from there to the end of the string: “World!”
2. substring(int beginIndex, int endIndex)
The method returns a new String object that contains the characters from the original string, starting from the beginIndex (inclusive) and optionally up to endIndex (exclusive).
Example:
public class SubstringTwoParametersExample {
public static void main(String[] args) {
String str = "Hello, World!";
// Extract substring from index 0 to index 5 (exclusive)
String subStr2 = str.substring(0, 5);
System.out.println(subStr2); // Output: Hello
}
}
Explanation: In this example, substring(0, 5) extracts the substring starting at index 0 and ending at index 4, which gives the substring “Hello”.
3. Handle Invalid Index
public class SubstringExample {
public static void main(String[] args) {
String str = "Hello World!";
try {
String subStr = str.substring(15, 5);
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage()); // Output: String index out of range: 15 to 5
}
}
}
Explanation: Here, attempting to extract a substring where the beginIndex (15) is greater than endIndex (5) throws a StringIndexOutOfBoundsException.
Java String substring() method – Questions and Answers
Q 1: What is substring() method used for?
Ans: Extracts a part of the string.
Q 2: How many versions of substring() exist?
Ans: Two versions: one index and start-end index.
Q 3: Does substring() modify original string?
Ans: No, it returns a new string.
Q 4: Is end index inclusive?
Ans: No, end index is exclusive.
Q 5: What exception can substring() throw?
Ans: StringIndexOutOfBoundsException.
Java String substring() method – Objective Questions (MCQs)
Q1. What does the substring() method in Java return?
Q2. What is the correct syntax to get a substring from index 2 to 5 of a string str?
Q3. If String s = "Programming";, what will s.substring(3) return?
Q4. What happens if the starting index in substring() is greater than the ending index?
Q5. What type of value does substring() return?