欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lua程序设计第4版第10章课后练习答案

程序员文章站 2024-03-17 23:14:16
...

10.1

function f101(str,space)
    str = string.gsub(str,"%s*(.-)%s*$","%1") -- 去除空格
    str = str..space
    local t = {}
    for a in string.gmatch(str,".-"..space) do
        if a~=space then
            a = string.sub(a,1,-2)
            t[#t+1] = a
        end
    end
    return t
end

10.2
否,前面%d取反了,%U没有一致
10.3

function f102(s,t)
    for k,v in pairs(t) do
        print(k)
        if v then
            s = string.gsub(s,k,v)
        else
            s = string.gsub(s,k,"")
        end
    end
    print(s)
end

10.4

function trim(s)
    time1 = os.clock()
    s = string.gsub(s,"^%s*(.-)%s*$","%1")
    print(os.clock()-time1)
    return s
end
function trimf104(s)
    time1 = os.clock()
    s =string.match(s,"%S.*%S")
    print(os.clock()-time1)
    return s
end
place = string.rep(" ",10000)

105

function f105(s)
    print(s)
    s = string.gsub(s,"\\(.)",string.format("\\x%02X",string.byte("%1")))
    print(s)
    s = string.gsub(s,"(%w)",string.format("\\x%02X",string.byte("%1")))
    print(s)
end
f105("\\0\\1hello\\200")

10.6

10.7

相关标签: LUA