Edmond
Edmond

Reputation: 43

substring IndexOutOfBoundsException

How come executing the following

String p = "abcd";
System.out.print(p.substring(4));

Doesn't cause java.lang.IndexOutOfBoundsException? It just prints an empty String.

There are nothing on p.charAt(4) and the following

System.out.print(p.charAt(4));

indeed causes java.lang.IndexOutOfBoundsException.

Upvotes: 0

Views: 2863

Answers (5)

Luffy
Luffy

Reputation: 1

Because at the end of "abcd" there is an empty string available that's why

"abcd".substring(4) returns ""(empty String)

But there is no character present at the 4th index and it is expecting a charecter here that's why below statement is gonna give you IndexOutOfBoundException

"abce".charAt(4)

Upvotes: 0

Avinash
Avinash

Reputation: 2191

public class TestSubstring{
 public static void main(String args[]){
String p = "abcd";
System.out.print(p.substring(4));
 }
}

o/p : Blank_space

public class TestSubstring{
 public static void main(String args[]){
String p = "abcd";
System.out.print(p.substring(5));
 }
}

o/p: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1931) at TestSubstring.main(TestSubstring.java:4)

Upvotes: 0

shameer1101
shameer1101

Reputation: 504

Taken from docs.oracle.com

Examples:

 "unhappy".substring(2) returns "happy"
 "Harbison".substring(3) returns "bison"
 "emptiness".substring(9) returns "" (an empty string)

If you look at the third example, it is pretty clear that it's the exact same case as yours. This particular statement, "IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object", the second part after "or" is pretty interesting, it clearly says that if the beginIndex value is larger than the length of the String or the negative case, the Exception gets thrown.

Hope that helps.

Upvotes: 0

Let's look what substring(int) does:

public String substring(int beginIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    int subLen = value.length - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}

when the index is negative you get an StringIndexOutOfBoundsException

in this case, the char you give is the same as the string length, so you get a new string but empty

new String(value, beginIndex, subLen)

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074148

Because charAt expects there to be a character at index 4, and there isn't. But substring doesn't expect that; if there's no character at index 4, you get an empty string. If there were a character at index 4, you'd get a string with at least one character in it.

If you used .substring(5), you'd get the out-of-bounds exception, because that's assuming there's at least one character at index 4; the same assumption charAt makes.

Upvotes: 4

Related Questions