In Java API methods like:
String.substring(int beginIndex, int endIndex)
String.subSequence(int beginIndex, int endIndex)
Because:
object.length
(however the object implements this, eg size() etc) into the toIndex parameter - no need to add/subtract 1For example:
String lastThree = str.substring(str.length() - 3, str.length());
This way, it is very obvious what is happening in the code (a good thing).
EDIT An example of a C function that behaves like this is strncat
from string.h
:
char *strncat(char *dest, const char *src, size_t n);
The size_t
parameter's value corresponds to the java endPosition
parameter in that they are both the length of the object, but counting from 0 if they are the index, it would be one byte beyond the end of the object.