lua 補全IP地址位數

–IP地址補全
172.0.0.1 —> 172.000.000.001

local ip = '172.0.0.1';
local aa = '.';
function mysplit(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        local t={}
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                table.insert(t, str)
        end
        return t
end


function buweiIP(IP)
    local result = mysplit(ip,".");
	for k,v in pairs(result) do
		local _len = string.len(v);
		if _len == 2 then
			v = '0'..v;
		elseif _len == 1 then
			v = '00'..v;
		end
		aa = aa..'.'..v;
	end
	return aa
end



bb = string.sub(buweiIP(ip),3)
print(bb)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章