Pascal Scripting: CharLength

Prototype:

function CharLength(const S: String; const Index: Integer): Integer;

Description:

Returns the length in bytes (1 or 2) of the character in the specified string at the specified index.

Remarks:

In double-byte character sets (Chinese, Japanese, Korean), most non-ASCII characters occupy two bytes. Note that the second byte of a double-byte character -- known as the "trail byte" -- can be in the same range used by ASCII characters (below 128). Thus, when stepping through a string that may contain double-byte characters, such as a path or filename, care must be taken to not mistake trail bytes for single-byte ASCII characters.

Example:
function BackslashToSlash(const S: String): String;
var
  I: Integer;
begin
  Result := S;
  I := 1;
  while I <= Length(Result) do
  begin
    if Result[I] = '\' then
      Result[I] := '/';
    // Go to the next character. But do not simply increment I by 1.
    // Increment by CharLength() in case Result[I] is a double-byte character.
    I := I + CharLength(Result, I);
  end;
end;

...

begin
  // Show path of Common Files with backslashes changed to forward slashes
  MsgBox(BackslashToSlash(ExpandConstant('{cf}')), mbInformation, MB_OK);
end;