How would i split string by number

alrighty so basically how would i split a 1-4 digit number by half
i want to separate them into 2 different values such as seconds and minutes

i don’t really know how to use string.sub

yeah im making a timer

local time = "1234" 
local minutes = string.sub(time, 1, 2)
local seconds = string.sub(time, 3)
print(minute,second)
--output 12 34

but it gets complicated when trying to do 3 or less digit numbers
because the minutes is 12, and the seconds is 3 and i want seconds to be 23

local time = "123" 
local minutes = string.sub(time, 1, 2)
local seconds = string.sub(time, 3)
print(minutes,seconds)
--output 12 3
--desired output 1 23

and then yeah is basically the same for the rest

local time = "12" 
local minutes = string.sub(time, 1, 2)
local seconds = string.sub(time, 3)
print(minutes,seconds)
--output 12, 0
--desired output 0, 12

Why do you have an input of four numbers in a string like this in the first place? Why not store the time as a number, or as two numbers?

because its uh microwave, i don’t know how to figure out how to separate minutes and seconds when trying to input numbers(because its just pressing buttons and numbers appear)

local Time = 123

local Minutes = math.floor(Time / 60) % 60
local Seconds = math.floor(Time) % 60

print(Minutes, Seconds) -- output 2 3
1 Like

eh, i don’t want to convert it into minutes and seconds, i only just want to separate the values, its gonna look weird when displaying the time for the timer/microwave

Ah microwave makes sense. Something like this

local function test(s)
  local hours, mins = string.sub(s, 1, -3), string.sub(s, -2)
  print(hours, mins)
end

test("1") -- "" "1"
test("12") -- "" "12"
test("123") -- "1" "23"
test("1234") -- "12" "34"
test("12345") -- "123" "45"
1 Like

Just get the length of the string via # or string.len() and use that to determine which approach should be applied to the string.

thank you, thats what i exactly wanted!