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.
About Question enthuware.ocajp.i.v7.2.1155 :
Moderator: admin
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1155 :
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.
You might want to take a look at the this JavaDoc API description for substring
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 60
- Joined: Thu Apr 15, 2021 12:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1155 :
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.
- 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.
Who is online
Users browsing this forum: Bing [Bot] and 210 guests