I've noticed that some methods like the String's substring(int beginIndex, int endIndex) and StringBuilder's delete(int beginIndex, int endIndex), use the second parameter to signify that the substring or deletion should go to endIndex-1 and not endIndex. Is there a reason for this? It doesn't seem, at least to me, to make logical sense for these methods to indicate the method stops before the parameter instead of at the parameter unlike some other methods, in other classes. Some example snippets would be:
4: StringBuilder sb = new StringBuilder("abcdef"); 5: sb.delete(2, 4); 6: System.out.println(sb); //This would print abef instead of abf
4: String str = "abcdef"; 5: String newStr = str.substring(2, 4); 6: System.out.println(newStr); //This would print cd instead of cde
Does this seem strange to anyone else considering how some other methods with index parameters work? Is there a reason for this? If so, please explain it to me.
Edit to differentiate and clarify unique question: This question is different from the discussion on solely substring() as it is about all methods that use indexcies to indicate that the method should stop before the provided endIndex (exclusion instead of inclusion).