Type Function Library utf8.* Return value iterator Revision Release 2025.3721 Keywords utf8, UTF-8, Unicode, string, codes
Returns values so that the following construction will iterate over all characters in string s, where charpos is the position (in bytes) of each character and codepoint is the code point.
for charpos, codepoint in utf8.codes( s ) do
--
end
This function raises an error if it finds an invalid byte sequence.
utf8.codes( s )
String. The string to examine.
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