There are few possible reasons for this sort of thing:
As in JacquesB's answer it may simply be convenienceconvenience for the library user to call a single method rather than two methods and keep their code more succinct.
PerformancePerformance may be a consideration, calling. Calling
.substring(3)
will result in a new stingstring being created, and there foretherefore you are looping over the string twice, it is possible*possible 1 for.startsWith("bar", 3)
to be implemented without creating a temporary substring and to just loop through the string once..startsWith
has another tiny optimization point,: it can bailoutbail out early if the first character does not match,.indexOf
will need to keep checking the rest of the string.Better expressing intent,Expressing intent: while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.
As Phillipp points out, historyhistory can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**API 2.
* I have no idea if this is actually the case here but I'd be surprised if it does.1 I have no idea if this is actually the case here but I'd be surprised if it does.
** Probably not in this case though.2 Probably not in this case though.