This question already has an answer here:
-
Why is “out of range” not thrown for 'substring(startIndex, endIndex)'
6 answers
So, I am trying to figure out why String.substring(int startIndex) allows a start index that is out of bounds and does not throw OOB exception?
Here is the code I was testing this with :
public class testSub {
public static void main(String[] args) {
String testString = "D";
String newSub= testString.substring(1); //print everything FROM this index Up, right? Should that not be out of bounds? Yet I just get a blank.
System.out.println(newSub); //this works fine and prints a blank
System.out.println(testString.charAt(1)); // <Yet this throws OOB?
System.out.println(testString.lastIndexOf("")); // Gives me (1) but I just tried getting it? Should this not mean String length is 2?
}
}
I understand that substrings are substring(inclusive, exclusive), but 1 is clearly out of bounds, so WHY is it giving a blank space instead of throwing OOB, or how is it doing it? Is "" some special exception?
Okay people have been throwing java doc around but that doesn't really answer the question.
The reason you can do "D".substring(1)
is because you're asking for the string starting at index 1, to index 1 (exclusive). This is by definition the empty string as its size is 1-1=0. Every string has an empty string at the start, end and between every character. E.g. "" + "B" + "O" + "" + "B" + ""
= "BOB"
.
As per JavaDoc, the substring method will only throw exception if index is > length of the string object.
The chart method will throw exception only if index is NOT LESS than the length of the object.
Substring method(int beginIndex)
Parameters:
beginIndex the beginning index, inclusive.
Returns:
the specified substring.
Throws:
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String
ChartAT Method
Returns the char value at the specified index. An index
ranges from 0 to length() - 1. The first char value of the
sequence is at index 0, the next at index 1, and so on,
as for array indexing.
If the char value specified by the index is a surrogate,
the surrogate value is returned.
Specified by: charAt(...) in CharSequence
Parameters:
index the index of the char value.
Returns:
the char value at the specified index of this string.
The first char value is at index 0.
Throws:
IndexOutOfBoundsException - if the index
argument is negative or not less than the length of this
string.
object.
As the documentation say's:
* IndexOutOfBoundsException if
* <code>beginIndex</code> is negative or larger than the
* length of this <code>String</code> object.
Only if beginIndex
is larger then length
a IndexOutOfBoundsException
is thrown. In your case both values are equals.
Looking at the Javadoc for String.substring(int beginIndex)
for when it throws IndexOutOfBoundsException
:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex
is larger than the length of this String object, or beginIndex is
larger than endIndex.
Since the String length is 1 and you are calling it with 1, you are not getting the IndexOutOfBoundsException
.
Since your string is of length 1 and String is backed by char array, you just have char[0]
populated with D as value (Note array index starts from 0 and not 1). Now you are trying to access 1st index from string (indirectly char array) using charAt method like:
System.out.println(testString.charAt(1));
And hence you get IOOB exception.
if you want to access first character from string, you should use:
System.out.println(testString.charAt(0));
What Marcio said. Plus: The method lastIndexOf
is not what you think. It is the last index of the argument substring in this
. For an empty string argument it will always be length plus one!