8000 GitHub - DoooReyn/lua-string-interpolate at 358a81cde7f41ea371fa3ec1e3eded646992ab17
[go: up one dir, main page]

Skip to content

DoooReyn/lua-string-interpolate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Lua 字符串插值

在 Python 中,格式化字符串可以使用多种形式,其中一种是字符串插值,如下:

name = "DoooReyn"
 "I am {name}".format(name=name)
# output
# I am DoooReyn,我来自中国

于是就想 Lua 可不可以这么搞。那么就来尝试一下吧:

  • 首先,定义下提取变量的规范:{var},模式匹配为:{%w+}
  • 接下来,准备解析格式化字符串,可以使用 Lua 的 string.gsub
  • 最后,提取到变量名之后,进行替换操作即可

于是得到:

function string.interpolate(fmt, keys)
    keys = type(keys) == "table" and keys or {}
    local ret =
        string.gsub(
        fmt,
        '{%w+}',
        function(c)
            local key = string.match(c, "(%w+)")
            // 添加数字索引支持
            key = tonumber(key) or key
            return keys[key] or ''
        end
    )
    return ret
end

测试一下:

print(string.interpolate('Hi! {who} am {name}, {who} am from {from}', {who = 'I', name = 'DoooReyn', from = 'China'}))
-- output: Hi! I am DoooReyn, I am from China
print(string.interpolate('Hi! {1} am {2}, {1} am from {3}', {'I', 'DoooReyn', 'China'}))
-- output: Hi! I am DoooReyn, I am from China

完美!

About

Lua 字符串插值

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0