Lua格式化分离字符串

项目中经常会遇到分离字符串 如"2019-11-23 12:23:56" 那怎么将这些数字分离出来呢,下面直接上代码讲解。其他情况也类似

function split(input, delimiter)
   input = tostring(input)
    delimiter = tostring(delimiter)
    if (delimiter=='') then return false end
    local pos,arr = 0, {}
    -- for each pider found
    for st,sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
        
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end

function transfromDateToTime(datestr)
	if not datestr or datestr == "" then return 0 end
    local datelist = split(datestr, " ")
    local datebegin = split(datelist[1], "-")
    local dateend = split(datelist[2], ":")
    local day = tonumber(datebegin[3])
    local month = tonumber(datebegin[2])
    local year = tonumber(datebegin[1])
    local hour = tonumber(dateend[1])
    local minute = tonumber(dateend[2])
    local second = tonumber(dateend[3])

    print(year,month,day,hour,minute,second)
end

transfromDateToTime("2010-1-14 11:07:56")
发布了43 篇原创文章 · 获赞 1 · 访问量 2305

猜你喜欢

转载自blog.csdn.net/lpl312905509/article/details/103969863