String substring index could be the length of stri

2019-01-12 08:22发布

问题:

This is a Java string problem. I use the substring(beginindex) to obtain a substring. Considering String s="hello", the length of this string is 5. However when I use s.substring(5) or s.substring(5,5) the compiler didn't give me an error. The index of the string should be from 0 to length-1. Why it doesn't apply to my case? I think that s.substring(5) should give me an error but it doesn't.

回答1:

Because the endIndex is exclusive, as specified in the documentation.

IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.


I think when I use s.substring(5), it should give me error while it didn't

Why would it be?

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.

Since the beginIndex is not larger than the endIndex (5 in your case), it's perfectly valid. You will just get an empty String.

If you look at the source code:

1915  public String substring(int beginIndex) {
1916      return substring(beginIndex, count);
1917  }
....
1941  public String substring(int beginIndex, int endIndex) {
1942      if (beginIndex < 0) {
1943          throw new StringIndexOutOfBoundsException(beginIndex);
1944      }
1945      if (endIndex > count) {
1946          throw new StringIndexOutOfBoundsException(endIndex);
1947      }
1948      if (beginIndex > endIndex) {
1949          throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
1950      }
1951      return ((beginIndex == 0) && (endIndex == count)) ? this :
1952          new String(offset + beginIndex, endIndex - beginIndex, value);
1953  }

Thus s.substring(5); is equivalent to s.substring(5, s.length()); which is s.substring(5,5); in your case.

When you're calling s.substring(5,5);, it returns an empty String since you're calling the constructor(which is private package) with a count value of 0 (count represents the number of characters in the String):

644 String(int offset, int count, char value[]) {
645         this.value = value;
646         this.offset = offset;
647         this.count = count;
648 }


回答2:

Because substring is defined as such, you can find it in the Javadoc of String.substring

@exception IndexOutOfBoundsException if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

It's useful in many cases that you can always create a substring that starts after a character in a String.

Because endIndex can be the length of the string, and beginIndex can be as large as endIndex (but not larger), it is also okay for beginIndex to be equal to the length of the string.



回答3:

In first case (s.substring(5)), Oracle docs says

...
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
...

In second case (s.substring(5,5)), it says that

...
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex
...