Garry's Mod Wiki

utf8.offset

  number utf8.offset( string string, number n, number startPos = 1 when n>=0, -1 otherwise )

Description

Returns the byte-index of the n'th UTF-8-character after the given startPos (nil if none). startPos defaults to 1 when n is positive and -1 when n is negative. If n is zero, this function instead returns the byte-index of the UTF-8-character startPos lies within.

Arguments

1 string string
The string that you will get the byte position from.
2 number n
The position to get the beginning byte position from.
3 number startPos = 1 when n>=0, -1 otherwise
The offset for n.

Returns

1 number
Starting byte-index of the given position.

Example

Returns the byte-index where the character at the 5th byte begins.

print(utf8.offset("( ͡° ͜ʖ ͡°)", 5))
Output: 7

Example

Safely truncates the string that may contain UTF-8 characters. The first print demonstrates the problem of string.sub

local s = 'Текст - Cyrillic text example' print(string.sub(s,1,5)) print(string.sub(s,1,utf8.offset(s,5)))
Output: Те? Текст