Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1155 :
Posted: Wed Feb 20, 2013 11:13 am
by ETS User
I see that one is correct but I don't understand how it works.
s = "MINIMUM" has starting index of 0 and ending in 6
How does it accept s.substring(4,7);
Thanks.
Re: About Question enthuware.ocajp.i.v7.2.1155 :
Posted: Wed Feb 20, 2013 5:27 pm
by admin
Because the rule is that endIndex must not be larger than the length. Here, length is 7 so endIndex can be 7.
You might want to take a look at the
this JavaDoc API description for substring
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1155 :
Posted: Wed Jul 14, 2021 4:34 am
by enthunoob
For others who are also confused with startindex or endindex, and inclusive or exclusive, I think these two rules will help to remember:
- Arrays are always zero based, so the first character of a string (which is an array of characters) always has an index of 0.
- a startindex is always inclusive and an endindex is always exclusive. This applies to all methods in Java, for example also to List.subList(int startindex, int endIndex).
This means that the index number of a first position is never 1.