As with the other substring method, this one
public String substring(int beginIndex, int endIndex )
creates a new string. The original string is not changed.There are some tricky rules.Essentially, the rules say that if the indexes make no sense,a IndexOutOfBoundsException
is thrown.
beginIndex
is negative value, an IndexOutOfBoundsException
is thrown.beginIndex
is larger than endIndex
, an IndexOutOfBoundsException
is thrown.endIndex
is larger than the length, an IndexOutOfBoundsException
is thrown.beginIndex
equals endIndex
, and both are within range, then an empty string is returned.Usually, when an exception is thrown your program halts.Future chapters discuss how to deal with exceptions
Decide on what string is formed by the following expressions: