2017年8月26日 星期六

Delphi - 文字長度(Length)


Example code : 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 =
'+IntToStrLength(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;


Show full unit code

  Length of openArray = 17
  Length of fixedArray = 3
  Length of multiArray = 3
  Length of shortStr = 8
  Length of longStr = 20
  Letter 1 = A

  Letter 2 = B
  Letter 3 = C
  Letter 4 = D
  Letter 5 = E
  Letter 6 = F
  Letter 7 = G
  Letter 8 = H

沒有留言:

張貼留言