Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "out of range" not thrown for 'substring(startIndex, endIndex)'

Tags:

java

substring

In Java I am using the substring() method and I'm not sure why it is not throwing an "out of index" error.

The string abcde has index start from 0 to 4, but the substring() method takes startIndex and endIndex as arguments based on the fact that I can call foo.substring(0) and get "abcde".

Then why does substring(5) work? That index should be out of range. What is the explanation?

/*
1234
abcde
*/
String foo = "abcde";
System.out.println(foo.substring(0));
System.out.println(foo.substring(1));
System.out.println(foo.substring(2));
System.out.println(foo.substring(3));
System.out.println(foo.substring(4));
System.out.println(foo.substring(5));

This code outputs:

abcde
bcde
cde
de
e
     //foo.substring(5) output nothing here, isn't this out of range?

When I replace 5 with 6:

foo.substring(6)

Then I get error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
    String index out of range: -1
like image 424
Meow Avatar asked Jul 13 '10 00:07

Meow


People also ask

What does string index out of range mean Java?

The string index out of range means that the index you are trying to access does not exist. In a string, that means you're trying to get a character from the string at a given point. If that given point does not exist , then you will be trying to get a character that is not inside of the string.

How do I get a substring?

The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string.

What is indexOf in Java?

Definition and Usage. The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.


2 Answers

According to the Java API doc, substring throws an error when the start index is greater than the Length of the String.

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

In fact, they give an example much like yours:

"emptiness".substring(9) returns "" (an empty string)

I guess this means it is best to think of a Java String as the following, where an index is wrapped in |:

|0| A |1| B |2| C |3| D |4| E |5|

Which is to say a string has both a start and end index.

like image 134
Matt Mitchell Avatar answered Oct 18 '22 15:10

Matt Mitchell


When you do foo.substring(5), it gets the substring starting at the position right after the "e" and ending at the end of the string. Incidentally, the start and end position happen to be the same. Thus, empty string. You can think of the index as being not an actual character in the string, but a position in between characters.

        ---------------------
String: | a | b | c | d | e |
        ---------------------
Index:  0   1   2   3   4   5
like image 44
Jeff Avatar answered Oct 18 '22 17:10

Jeff