Nicholas Rubin
Nicholas Rubin

Reputation: 327

Pieces of strings in Lua

I prompt the user to input a variable 'year'.

I need to get the first 2 digits, and the last 2 digits of the 4-digit variable year for the Gaussian algorithm and store them in two separate variables.

What I need help on is getting the first 2 and last 2 numbers from 'year'.

Upvotes: 0

Views: 90

Answers (2)

Mud
Mud

Reputation: 28991

To add to Bart's answer, if you're keeping it in string form you can just use sub to grab the parts you want:

year = '1969'
firsttwo = year:sub(1,2)
secondtwo = year:sub(3,4)

Upvotes: 1

Bart Kiers
Bart Kiers

Reputation: 170178

Try this:

local n = 1234
local first = math.floor(n / 100)
local last = n % 100

print(first, last)

which print:

12  34

Upvotes: 1

Related Questions