lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 6 March 2010 11:29, David Burgess <dabsoft@gmail.com> wrote:
It seems to me that negative values of string.sub() are inconsistent with not only the first parameter but also with string.find().

Examples:

>s="123456789"
>=s:sub(-1)
9
>=s:sub(1,0)

>=s:sub(1,-1)
123456789
>=s:sub(1,-2)
12345678
>=s:find("56")
5       6
>=s:sub(1,6-2)
1234
>=s:sub(-1)
9

I am boldly suggesting that the second parameter with negative indices behave the same as the first. I guess this implies that the value 0 for
the second parameter would return the length of the string.
Maybe I have used too many similar functions in other languages but -1 meaning the last character seems intuitive to me.

Anyone else of a like view?

DB

I don't understand what your question/proposal is..... everything seems normal/sensible to me.
1 is the first character; -1 is the last character....