Type Function
Library utf8.*
Return value Numbers
Revision 2018.3333
Keywords utf8, UTF-8, Unicode, string, charpos
See also utf8.codepoint()


Converts UTF-8 position to byte offset depending on usage:

In both cases, this function returns a new character position and code point (number) at this position.


utf8.charpos( s, [charpos,] offset )
s (required)

String. Any string.

charpos (optional)

Number. The character position in the UTF-8 string.

offset (required)

Number. The character offset.


local utf8 = require( "plugin.utf8" )

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

print( utf8.sub( testStr, 3, 3 ) )  --> 你

print( utf8.charpos( testStr, 3 ) )  --> 5  20320