utf8.codes()

类型 函数
utf8.*
返回值 迭代器
修订 版本 2024.3703
关键词 utf8, UTF-8, Unicode, 字符串, codes

概述

返回一些值,以便以下构造将迭代字符串 s 中的所有字符,其中 charpos 是每个字符的位置(以字节为单位),codepoint 是代码点。

for charpos, codepoint in utf8.codes( s ) do
    --
end

如果发现无效的字节序列,此函数将引发错误。

语法

utf8.codes( s )
s (必填)

字符串 要检查的字符串。

示例

local utf8 = require( "plugin.utf8" )

local testStr = "♡ 你好,世界 ♡"

for charpos, codepoint in utf8.codes( testStr ) do
    print( charpos, codepoint )
end

--> 1   9825
--> 4   32
--> 5   20320
--> 8   22909
--> 11  65292
--> 14  19990
--> 17  30028
--> 20  32
--> 21  9825