characterIndexFromByteOffset

characterIndexFromByteOffset(offset: Integer): Integer;

The characterIndexFromByteOffset method of the StringUtf8 primitive type returns the index of the character that starts at the byte offset specified in the byte parameter within the receiving UTF8 string, or after that offset; that is, the method scans from the offset position forwards to find the next character. If there is no next character, an exception is raised.

In the following code example, the two characters of the string str8 require two bytes and three bytes in the UTF8 encoding. The first character starts at offset one (1) and the second character at offset three (3).

vars
    str8: StringUtf8;
begin
    str8 := @"©€";
    write str8.characterIndexFromByteOffset(1);     // writes 1
    write str8.characterIndexFromByteOffset(2);     // writes 2
    write str8.characterIndexFromByteOffset(3);     // writes 2
    write str8.characterIndexFromByteOffset(4);     // raises 1413 exception
    write str8.characterIndexFromByteOffset(5);     // raises 1413 exception
end;