Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua string.sub() result when end index is less than the initial index

In all the Lua definitions of string.sub I could not find what it returns when the end index is positive yet less than than the initial index.

For example, will string.sub(someString, 3, 2) always return the empty string ""?

like image 475
Linus Avatar asked Aug 30 '25 15:08

Linus


1 Answers

Yes.

Refer to: string.sub (s, i [, j]):

If (...) i is greater than j, the function returns the empty string.

i and j are respectively first and second indices from arguments.

Note that while 5.1's manual doesn't mention this behaviour (documentation for string.sub was extended in 5.2), the implementation didn't change in a meaningful way: 5.1, 5.2, 5.3 or 5.4; the behaviour is persistent across those versions.

like image 160
Green Avatar answered Sep 02 '25 11:09

Green