Is SpannableString.setSpan() 2nd parm 0-based?
Asked Answered
A

2

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?)

Artless answered 11/5, 2011 at 18:38 Comment(0)
P
9

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

Parrish answered 11/5, 2011 at 18:45 Comment(1)
So, if I understand you correct, to cover a String whose length is exactly 4 characters, start=0 and end=4?Artless
K
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.

Katheryn answered 11/5, 2011 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.