Name

Length Function

Syntax

function Length(const S: String): Integer;
function Length(const A: array): Integer;

Description

Length returns the number of elements in a string or array. It is not a real function.

Tips and Tricks

  • Although Length is most often used to learn the length of a dynamic array, you can also call Length to find the length of an open array parameter or even a static array.

  • For an array, Length(A) always returns Ord(High(A)) - Ord(Low(A)) + 1. For a ShortString, however, Length returns the value stored in the length byte, which might be shorter than the string size.

Example

// Toggle case of a string.
procedure ToggleCase(var S: string);
var
  I: Integer;
begin
  for I := 1 to Length(S) do
    if S[I] in ['a'..'z'] then
      S[I] := UpCase(S[I])
    else if S[I] in ['A'..'Z'] then
      S[I] := DownCase(S[I]);
end;

See Also

High Function, Low Function, SetLength Procedure, SizeOf Function

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.