7

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.

2
  • s.substring(s.length()) is silly but valid Commented Mar 29, 2014 at 11:17
  • 4
    "I think when I use s.substring(5), it should give me error while it didn't" - Don't rely on your intuition. Read the javadocs. They say it shouldn't. Commented Mar 29, 2014 at 11:18

3 Answers 3

9

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 }
Sign up to request clarification or add additional context in comments.

2 Comments

but it didn't have the index ==5. but I can use s.substring(5)?why
@user3382017 s.substring(5); is equivalent to s.substring(5, s.length()); which is s.substring(5,5); for "Hello".
6

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.

1 Comment

+1. You just taught me something. minIndex can be equal to length. I never did it, so I assumed it was illegal.
1

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
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.