The substring() method in Java is used to extract a portion of a String. It returns a new String that is a substring of the original string. The method is overloaded, allowing you to specify either one or two indices to determine the start and end of the substring.
Key Features of substring() Method:
- Method Signatures:
String substring(int beginIndex)String substring(int beginIndex, int endIndex)- Parameters:
beginIndex: The starting index (inclusive) where the substring begins. Indexing starts at 0.endIndex: The ending index (exclusive) where the substring ends. This parameter is optional in the overloaded version, and if omitted, the substring will extend to the end of the original string.- Return Value:
- The method returns a new
Stringthat is the substring of the original string starting frombeginIndexand ending atendIndex - 1. IfendIndexis not specified, the substring goes frombeginIndexto the end of the string. - Important Notes:
- If
beginIndexis equal toendIndex, the method returns an empty string. - If
beginIndexorendIndexare out of range (less than 0, greater than the length of the string, or ifbeginIndexis greater thanendIndex), the method throws aStringIndexOutOfBoundsException.
Examples of Using substring() Method:
- Extracting a Substring with One Parameter:
java
String str = "Hello, World!";
String substr = str.substring(7); // Starts from index 7 to the end
System.out.println(substr); // Output: "World!"
2. Explanation: Here, substring(7) returns the substring starting from index 7 to the end of the string.
3. Extracting a Substring with Two Parameters:
java
String str = "Hello, World!";
String substr = str.substring(7, 12); // Starts from index 7 and ends at index 11
System.out.println(substr); // Output: "World"
4. Explanation: Here, substring(7, 12) returns the substring starting from index 7 and ending at index 11 (endIndex is exclusive).
5. Extracting an Empty Substring:
java
String str = "Hello, World!";
String substr = str.substring(5, 5); // beginIndex and endIndex are the same
System.out.println(substr); // Output: ""
6. Explanation: Since beginIndex is equal to endIndex, the method returns an empty string.
7. Handling StringIndexOutOfBoundsException:
java
String str = "Hello, World!";
try {
String substr = str.substring(7, 50); // endIndex is out of range
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Index out of bounds: " + e.getMessage());
}
8. Explanation: This code snippet catches the StringIndexOutOfBoundsException because endIndex is greater than the length of the string.
Conclusion:
The substring() method in Java is a powerful tool for extracting specific portions of a string. By specifying beginIndex and optionally endIndex, you can retrieve substrings from the original string. It is important to handle potential exceptions, particularly StringIndexOutOfBoundsException, to ensure your code is robust. Understanding how to use substring() effectively allows you to manipulate strings more precisely and efficiently in Java.