Linus
Linus

Reputation: 1614

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

Upvotes: 4

Views: 581

Answers (1)

Aki
Aki

Reputation: 2938

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.

Upvotes: 5

Related Questions