Java 21 adds new String.indexOf methods that accept ranges (beginIndex, to endIndex) rather than just a
start index. A StringIndexOutOfBounds can be thrown when indicating an invalid range, namely when:
beginIndex > endIndex (eg: beginIndex and endIndex arguments are mistakenly reversed) beginIndex < 0 (eg: because the older String.indexOf(what, fromIndex) accepts negative values) String.indexOf(what, beginIndex, endIndex) instead of String.indexOf(what, endIndex, beginIndex). String.indexOf(what, 0, endIndex) instead of String.indexOf(what, -1, endIndex).
String hello = "Hello, world!";
int index = hello.indexOf('o', 11, 7); // Noncompliant, 11..7 is not a valid range
String hello = "Hello, world!";
int index = hello.indexOf('o', 7, 11); // Compliant
String hello = "Hello, world!";
int index = hello.indexOf('o', -1, 11); // Noncompliant, because beginIndex is negative
String hello = "Hello, world!";
int index = hello.indexOf('o', 0, 11); // Compliant