I was wondering why Java and other programming languages implement (seemingly) redundant functions such as:foobar.startsWith(bar, 3);// same asfoobar.substring(3).startsWith(bar);// or even indexOf(str, offset), regionMatches(...)Is there a reason behind this?
Why does Java (and other langs too) have seemingly redundant functions like str.startsWith(String str, int fromOffset)?
programming languages;language agnostic;language design
There are few possible reasons for this sort of thing:As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.Performance may be a consideration. Calling .substring(3) will result in a new string being created, and therefore you are looping over the string twice, it is 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 bail out early if the first character does not match, .indexOf will need to keep checking the rest of the string.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, history 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 2.1 I have no idea if this is actually the case here but I'd be surprised if it does. 2 Probably not in this case though. 