utf8.charpos()

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

Overview

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.

Syntax

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.

Example

local utf8 = require( "plugin.utf8" )

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

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

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