DEV Community

Cover image for The weird substring quirk
Vineeth
Vineeth

Posted on

The weird substring quirk

Every once in a while have you ever come across a quirk in a programming language, which you quite do not understand, but yet come to terms with it.

One such quirk is the substring API which is present in Java. For starters, substring is an overloaded function - the result; there are 2 different forms of the same function.

public String substring(int beginIndex)
Enter fullscreen mode Exit fullscreen mode

Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

public String substring(int beginIndex, int endIndex)
Enter fullscreen mode Exit fullscreen mode

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

String str = "Hello, World";
String substringOne = str.substring(7);
String substringTwo = str.substring(0, 5);
Enter fullscreen mode Exit fullscreen mode

We can quite confidentally say that the output for substringOne will be "World". But we would be wrong if we were to say the output of substringTwo would be "Hello,".

The reason being the endIndex is exclusive. The inclusion/exclusive model is standard for ranges of the Java API. One of the main advantage is that the length of the string can be used as the end point without subtracting the "-1" from the length.

String apollo13 = "Houston, we have a problem";
String normalPeople = apollo13.substring(9, apollo13.length());
Enter fullscreen mode Exit fullscreen mode

But, is this really necessary? Java already provides an overload to the substring method which captures exactly this behaviour. All I’m trying to say is that I’ve seen many coders get tripped up. I have seen most people forget about exclusion when providing the endIndex.

Let me know if you feel the same or if I am totally off base here...

Top comments (0)