Reputation: 2590
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
Upvotes: 6
Views: 239
Reputation: 59111
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.
Upvotes: 7