Search code examples
stringluasubstring

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


Solution

  • 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.