![]() |
VOOZH | about |
The substring() method of the StringBuilder class is used to extract a portion of characters from an existing character sequence and return it as a new String. It always returns a new String object and does not modify the original StringBuilder. Indexing is zero-based, and passing invalid index values results in runtime exceptions.
Example.
Substring = ForGeeks
Explanation: Index 5 points to the character 'F'. All characters from index 5 to the end are returned as a new String.
public String substring(int start, int end)
Parameter:
Return Type: String
Substring = ForGeeks
Explanation:
For
Explanation:
Passing a negative index or an index greater than the length of the StringBuilder causes a StringIndexOutOfBoundsException at runtime.
java.lang.StringIndexOutOfBoundsException: Range [-3, 13) out of bounds for length 13
Explanation: Negative start index is invalid. The method throws "StringIndexOutOfBoundsException" at runtime.