Another way to look at this is that
str.substring(0, x) + str.substring(x)
will always be equivalent to s , where 0 <= x <= str.length()
For this, for a break for the only case where x == str.length() will be inconsistent and annoying - for example, you will have to write special cases in analysis loops.
See also the documentation for StringIndexOutOfBoundsException :
Created by the String method to indicate that the index is either negative or larger than the size of the string. For some methods, such as the charAt method, this exception is also thrown when the index is equal to the size of the row.
Please note that the second sentence - charAt should throw an exception when the index is equal to the length of the string, because there is no char at this position for this position. But technically, there is a valid String in this position - this is the zero-length of the String , i.e. "" .
This is consistent with other "slice" operations in java - for example,
list.subList(list.size(), list.size())
returns an empty list, but does not throw an exception.
source share