utf8.next()

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

概述

根据用法检查或迭代UTF-8字符串

for charpos, codepoint in utf8.next, "UTF8-string" do
    print( charpos, codepoint )
end

在所有情况下,此函数都会返回一个新字符位置(以字节为单位)和该位置的代码点(数字)。

语法

utf8.next( s [, charpos [, offset]] )
s (必需的)

字符串 字符串。

charpos (可选)

数字 要开始的字符位置。

offset (可选)

数字 字符偏移量。

示例

下一个偏移量
local utf8 = require( "plugin.utf8" )

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

print( utf8.next( testStr, 2 ) )  --> 3  161
迭代器
local utf8 = require( "plugin.utf8" )

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

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

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