Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java uses one past index for upper bound in string operations?

Tags:

java

string

In String functions like substring()

"hello".substring(0, 3)

returns hel
whereas 0-3 index includes hell

and in regex Matcher's end() method

mat = Pattern.compile("test").matcher("test");
mat.find();
System.out.println(mat.end());

returns 4 whereas first match ends at index 3

I'm just curious about why java works in this way

like image 562
Rajat Avatar asked Mar 09 '18 09:03

Rajat


Video Answer


1 Answers

For one thing, because the endpoint is excluded, s.substring(i,j) has length j-i (which is intuitively correct).

For another, s.substring(i,j) + s.substring(j,k) is equal to s.substring(i,k) (for sensible values of i, j and k).

Also, s.substring(0, s.length()) describes the whole of s, as it should, because the endpoint is excluded.

If the endpoint was included, you'd be continually having to remember to add or subtract one from things in order to make stuff work.

Matcher.end() is consistent with these: start-inclusive, end-exclusive, so s.substring(m.start(), m.end()) gives you the substring matched.

like image 176
khelwood Avatar answered Oct 20 '22 07:10

khelwood