22

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

35

Use string.sub with negative index.

Example

string.sub("Hello world", -5) -- => world
Sign up to request clarification or add additional context in comments.

Comments

8

You can also use this variant:

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

2 Comments

Does this have any advantage or disadvantage related to the accepted answer?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.