RAD Studio VCL Reference
AnsiStrings.CharLength Function

Get number of bytes required by character.

function CharLength(const S: AnsiString; Index: Integer): Integer; overload;
int CharLength(const AnsiString S, int Index);

CharLength returns the number of bytes required by the character in S starting at Index. If the character does not start at Index, this function returns the size of the remainder of the character, not the full character length.

Note: Index is an element index into S, not a byte or character index.
If the system is not using a multi-byte character system (MBCS), CharLength always returns 1. 

The following example illustrates CharLength's operation.

            SJISString = type AnsiString(932);
            A: SJISString;
            L: Integer;
            A := 'A' +
            'B' +
            #$82#$A0 +  // Japanese Hiragana 'A'
            #$82#$A2 +  // Japanese Hiragana 'I'
            #$82#$A4 +  // Japanese Hiragana 'U'
            'C' +
            L := CharLength(A, 1);     //returns 1 ('A')
            L := CharLength(A, 2);     //returns 1 ('B')
            L := CharLength(A, 3);     //returns 2
            L := CharLength(A, 4);     //returns 1

In this example, when the index is 1 or 2, it points to the beginning of a single byte character, so the function returns 1. When the index is 3, it points to the beginning of a two byte character, and the function returns 2. When the index is 4, it points to the second half of a two byte character and returns 1. Note that for this example, the element size is 1. Some characters require two elements, and some only need one element. 

In general, CharLength is only useful when you start indexing at the beginning of a string and note where multibyte characters start as the index increases. 


Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!