国际访客建议访问 Primers 编程伙伴 国际版站点 > Lua 教程 > utf8-module 以获得更好的体验。

# Lua 的 UTF-8 模块

请查看 Lua 标准库模块列表 了解更多相关 API。
变量 说明
utf8.charpattern 匹配一个 UTF-8 序列的模式字符串:"[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
函数 说明
utf8.char) 由 UTF-8 编码生成 UTF-8 字符串
utf8.codes) 读取 UTF-8 字符串的编码
utf8.codepoint) 读取 UTF-8 字符串的编码
utf8.len) 获取 UTF-8 字符串的长度
utf8.offset) 获取 UTF-8 字符串的长度

# utf8.char

utf8.char (···)

!subtitle:说明

通过 UTF-8 编码生成 UTF-8 字符串。

!subtitle:参数

  • ... - UTF-8 编码

!subtitle:返回值

  • 返回生成的 UTF-8 字符串

!subtitle:示例

运行示例

local text = utf8.char(
    -- "Primers " (ASCII)
    80, 114, 105, 109, 101, 114, 115, 32,
    
    -- "编程伙伴" (中文)
    0x7F16,  -- 编
    0x7A0B,  -- 程
    0x4F19,  -- 伙
    0x4F34,  -- 伴
    
    32,  -- 空格
    
    -- "https://xplanc.org" (ASCII)
    104, 116, 116, 112, 115, 58, 47, 47, 120, 112, 108, 97, 110, 99, 46, 111, 114, 103
)

print(text)

# utf8.codes

utf8.codes (s [, lax])

!subtitle:说明

将 UTF-8 字符串 s 按照拆分为 UTF-8 编码序列,返回迭代器函数。

!subtitle:参数

  • s - 要操作的 UTF-8 字符串

  • lax - 是否提升编码检查范围;默认 false

!subtitle:返回值

  • 返回迭代器函数,每次迭代返回一个索引和 UTF-8 字符编码

!subtitle:示例

运行示例

for pos, code in utf8.codes("Primers 编程伙伴") do
    print(pos, code, utf8.char(code))
end

# utf8.codepoint

utf8.codepoint (s [, i [, j [, lax]]])

!subtitle:说明

将 UTF-8 字符串 s 拆分为 UTF-8 编码序列。

!subtitle:参数

  • s - 要操作的 UTF-8 字符串

  • i - 开始位置(字节);默认为 1

  • j - 结束位置(字节);默认为 i

  • lax - 是否提升编码检查范围;默认 false

!subtitle:返回值

  • 返回 UTF-8 编码序列

!subtitle:示例

运行示例

print(utf8.codepoint("Primers 编程伙伴", 1, -1))

# utf8.len

utf8.len (s [, i [, j [, lax]]])

!subtitle:说明

获取 UTF-8 字符串 s 的长度。

!subtitle:参数

  • s - 要操作的 UTF-8 字符串

  • i - 开始位置(字节);默认为 1

  • j - 结束位置(字节);默认为 -1

  • lax - 是否提升编码检查范围;默认 false

!subtitle:返回值

  • 返回 UTF-8 字符串长度

!subtitle:示例

运行示例

print(utf8.len("编程伙伴"))

# utf8.offset

utf8.offset (s, n [, i])

!subtitle:说明

获取 UTF-8 字符串 s 中第 i 字节之后的第 n 个字符的字节偏移量。

!subtitle:参数

  • s - 要操作的字符串

  • n - 第 n 个字符

  • i - 开始位置(字节);默认为 1

!subtitle:返回值

  • 返回字节偏移量

!subtitle:示例

运行示例

print(utf8.offset("编程伙伴", 3))       -- 第三个 UTF-8 字符(伙)的偏移量
print(utf8.offset("编程伙伴", 3, 4))    -- 第四字节后第三个 UTF-8 字符(伴)的偏移量

# 推荐阅读

本文 更新于: 2025-11-27 09:38:13 创建于: 2025-11-27 09:38:13