A StringIndexOutOfBoundsException is thrown when a String or StringBuffer object detects an out-of-range index. An out-of-range index occurs when the index is less than zero, or greater than or equal to the length of the string. Using the substring method, a subset of the character sequence can be extracted from a string. The substring index must be any value from 0 to the length of a string. If the index exceeds the limit, StringIndexOutOfBoundsException is thrown in String substring method.
How to solve the StringIndexOutOfBoundsException
- Check the length of the string.
- Exception handling using try...catch.