Hi,
This is not a fulfilling reply i guess, but i checked the api and found that this difference is due to the definitions of substring() and charAt.
Check it out
The substring() method does indeed throw an exception in the following cases :
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
Unfortunately, it doesn't check the case if the beginindex(in fact the only index is greater than string length.)
On the other hand, check out charAt()
in the explanation of StringIndexOutOfBoundsException.
public class StringIndexOutOfBoundsException
extends IndexOutOfBoundsException
Thrown by the charAt method in class String and by other String methods to indicate that an index is either negative or greater than or equal to the size of the string.
After i typed the above i checked the charAt() in the api
doc.
It says, in case of charAt(int beginIndex) , it throws the StringIndexOutofBoundsException if beginIndex is greater than String length. So, if you change the parameter to charAt(5), it indeed throws it.(I checked out) . The case of the parameter being equal to the string length is really an anomaly.
I have added the relevant portion of the API doc
substring
public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Examples:
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string) Parameters:
beginIndex - the beginning index, inclusive.
Returns:
the specified substring.
Throws:
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
------------------
Regards,
Shree
[This message has been edited by shree vijay (edited December 10, 2000).]