20

I would like to extract the last N characters from a Lua string.

What way I could use to achieve this?

1

2 Answers 2

34

Use string.sub with negative index.

Example

string.sub("Hello world", -5) -- => world
8

You can also use this variant:

('Hello world'):sub(-5)  --> world
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.