8

My empirical experiment shows that if I setSpan(o, start, end) from start to end of a string, where end is String.length() - 1, the last character isn't covered.

When I changed end to exactly String.length(), the entire string is covered and... I don't even get an "out-of-bound" exception.

Unfortunately, there is nothing in the documentation regarding this particular issue.

Can you confirm my observation? (or prove me wrong?)

uTubeFan
  • 6,664
  • 12
  • 41
  • 65

2 Answers2

9

end is exclusive. 0, 2, for example, is everything from 0 inclusive to 2 exclusive which is 0 and 1.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • So, if I understand you correct, to cover a String whose length is exactly 4 characters, start=0 and end=4? – uTubeFan May 11 '11 at 18:48
2

Most "ending" indexes will be exclusive as a matter of practice. That is, the ending index is one beyond the index of the last whatever-it-is. If SpannableString is the one from the Android SDK, then no, the documentation doesn't say this for method setSpan, though it does for other methods such as subSequence.

Nathan Ryan
  • 12,893
  • 4
  • 26
  • 37