如何使用 string.format 和包含 UTF-8 字符的字符串获得“正确”格式?
\n\n例子:
\n\nlocal str = "\\xE2\\x88\\x9E"\nprint(utf8.len(str), string.len(str))\nprint(str)\nprint(string.format("###%-5s###", str))\nprint(string.format("###%-5s###", \'x\'))\nRun Code Online (Sandbox Code Playgroud)\n\n输出:
\n\n1 3\n\xe2\x88\x9e\n###\xe2\x88\x9e ###\n###x ###\nRun Code Online (Sandbox Code Playgroud)\n\n它看起来像string.format使用无穷大符号的字节长度而不是“字符长度”。\n是否有等效的 UTF-8 string.format?
function utf8.format(fmt, ...)\n local args, strings, pos = {...}, {}, 0\n for spec in fmt:gmatch\'%%.-([%a%%])\' do\n pos = pos + 1\n local s = args[pos]\n if spec == \'s\' and type(s) == \'string\' and s ~= \'\' then\n table.insert(strings, s)\n args[pos] = \'\\1\'..(\'\\2\'):rep(utf8.len(s)-1)\n end\n end\n return (\n fmt:format(table.unpack(args))\n :gsub(\'\\1\\2*\', function() return table.remove(strings, 1) end)\n )\nend\n\nlocal str = "\\xE2\\x88\\x9E"\nprint(string.format("###%-5s###", str)) --> ###\xe2\x88\x9e ###\nprint(string.format("###%-5s###", \'x\')) --> ###x ###\nprint(utf8.format ("###%-5s###", str)) --> ###\xe2\x88\x9e ###\nprint(utf8.format ("###%-5s###", \'x\')) --> ###x ###\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
6030 次 |
| 最近记录: |