Search results for 'element count in array'. 1 post(s) found.

  1. 2009/08/22 Get the length of arrays and a string
2009/08/22 22:18

Get the length of arrays and a string


The Length function returns either the number of characters in Source String, or the number of elements in Source Array.

Notes: Arrays start at index=0 by default, So the Length of such an array is 1 more than the highest index.

Following is the simple example to get the Length of arrays and a string
var
  openArray  : array of char;
  fixedArray : array[2..4] of Integer;
  multiArray : array[2..4, 1..9] of Integer;
  shortStr   : shortstring;
  longStr    : string;
  i          : Integer;

begin
  // Define the Length of the open array
  SetLength(openArray, 17);

  // Show the Length of the arrays
  ShowMessage('Length of openArray = '+IntToStr(Length(openArray)));
  ShowMessage('Length of fixedArray = '+IntToStr(Length(fixedArray)));
  ShowMessage('Length of multiArray = '+IntToStr(Length(multiArray)));

  // Assign to the strings
  shortStr := 'ABCDEFGH';
  longStr  := '12345678901234567890';
  ShowMessage('Length of shortStr = '+IntToStr(Length(shortStr)));
  ShowMessage('Length of longStr = '+IntToStr(Length(longStr)));

  // Display one letter at a time from the short string
  for i := 1 to Length(shortStr) do
    ShowMessage('Letter '+IntToStr(i)+' = '+shortStr[i]);
end;

Trackback 0 Comment 0

Trackback : Cannot send a trackbact to this post.