例如:
local a = "Lua"
local u = "???"
print(a:len(), u:len())
Run Code Online (Sandbox Code Playgroud)
输出:
3 6
Run Code Online (Sandbox Code Playgroud)
如何输出utf-8字符串中的字符数?
如果需要在Lua中使用Unicode/UTF-8,则需要使用外部库,因为Lua仅适用于8位字符串.一个这样的库是slnunicode.示例代码如何计算字符串的长度:
local unicode = require "unicode"
local utf8 = unicode.utf8
local a = "Lua"
local u = "???"
print(utf8.len(a), utf8.len(u)) --> 3 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3128 次 |
| 最近记录: |