I don't have any obvious operator to propose (ideally, it should be something that can be associated with the end of the string), but for the syntax, you just add it before the index, either after the leading [ or after the ... (One could also have it before the .., if one likes that symmetry better).
As for negation, there are two choices. I think I'd prefer to say that 0 is the position after the end of the string, -1 the position before that, and so on. Say we make the random choise of % for the magic character (any binary operator could be reused, as could for example:, ;, ,, /, @, $).
Then "banan"[%-3..%-1] == "nan", "banan"[%-4..4] == "ana". The easiest way to understand it is to think of %, in this context, as a shorthand for "sizeof(the string or array in question) +".
Actually, $ might be a better choice than %, anyone familiar with regexps can easily associate it to "end". Then it's
"banan"[$-3..$-1]
And when talking about ranges, I still like the old idea of using (x..y), [x..y) etc to represent open and half open intervals ;-)
/ Niels Möller (med röd bil)
Previous text:
2003-05-15 21:17: Subject: Negative ranges revisited
Perhaps, but what would that syntax be? '-' has the advantage that e.g.
foo = foo[..-2]
would do what most people intuitively think it does. Otoh one could easily consider it bogus that the above isn't the same as:
int n = -2; foo = foo[..n];
/ Martin Stjernholm, Roxen IS