0

I have to do bugfixes in a very old code (pre 2005) in which the below signature of String::substring is used.

public String substring(int beginIndex, int endIndex)

Now it contains every character from beginIndex till endIndex-1. Is it possible that in the past it also contained the character on endIndex?

In the code it looks like the following:

String str = line.substring(119,  120);

and it is compared to a 2 letter string. Does anyone have any information about this?

Pajti
  • 21
  • 2
  • 2
    In Java2, it is still endIndex excluded: https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#substring(int,%20int) – Willem Van Onsem May 22 '17 at 11:38
  • Nope not possible, I would say (note that javadoc of old java version is still available and public) –  May 22 '17 at 11:38
  • 1
    I would have to assume that this is the fault of the developer who wrote the code. Those working on Java have a strong commitment to backwards compatibility, and I wouldn't see them changing something like this. I've always known the ending index to be exclusive. – Logan May 22 '17 at 11:39
  • Looks like a bug in the original source code. And usually the developers of Java aim to keep old Java code running as much as possible. Since there are not that much reasons here to alter the semantics, I don't think it is very likely they did that. – Willem Van Onsem May 22 '17 at 11:39
  • I'm pretty sure that substring() behaviour in Java has always been the same. – Riaan Nel May 22 '17 at 11:40
  • That's probably one of the bugs you should fix :) – Thomas May 22 '17 at 11:47
  • Thanks to everyone :) – Pajti May 22 '17 at 12:06

2 Answers2

1

The signature or the working of the String#substring(int beginIndex, int endIndex) method has never changed.

I've been working on Java since around 2003 and I know it has been the same. You can verify which version of Java you have your code running on all these years and check the corresponding documentation from Oracle's Archives where you get docs for all the way back to Java 1.1:

http://www.oracle.com/technetwork/java/archive-139210.html

Take a look.

String#substring


Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
0

It seems that the substring method does the same thing since the beginning. Here you can see substring javadoc for jdk 1.0.

senerh
  • 1,315
  • 10
  • 20