Garry's Mod Wiki

utf8.len

  number, number utf8.len( string string, number startPos = 1, number endPos = -1 )

Description

Returns the number of UTF-8 sequences in the given string between positions startPos and endPos (both inclusive). If it finds any invalid UTF-8 byte sequence, returns false as well as the position of the first invalid byte.

Arguments

1 string string
The string to calculate the length of.
2 number startPos = 1
The starting position to get the length from.
3 number endPos = -1
The ending position to get the length from.

Returns

1 number
The number of UTF-8 characters in the string. If there are invalid bytes, this will be false.
2 number
The position of the first invalid byte. If there were no invalid bytes, this will be nil.

Example

Demonstrates output of this function compared to string.len when given a string that contains Russian text.

print( string.len( "Мёнём" ) ) print( utf8.len( "Мёнём" ) )
Output:
11 5