RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Length Function

Returns the number of characters in a string or elements in an array.

Pascal
function Length(S): Integer;
C++
int Length( S);

In Delphi code, Length returns the number of characters actually used in the string or the number of elements in the array. In C++ code, use the method of the same name on the AnsiString or DynamicArray class. 

For single-byte and multibyte strings, Length returns the number of bytes used by the string. For Unicode (WideString) strings, Length returns the number of bytes divided by two. 

S is a string- or array-valued expression.  

Delphi Examples: 

 

{
The following example uses an edit control, a button, and a
label on a form. When the button is clicked, the hexadecimal
value of each character in the edit control is displayed in
the label.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  Label1.Caption := '';
  for i := 1 to Length(Edit1.Text) do
  begin
    try
      Label1.Caption :=
        Label1.Caption +
        SysUtils.IntToHex(Byte(Edit1.Text[i]),2) + ' ';
    except
      Beep;
    end;
  end;
end;
{
The following OnFind event handler searches a memo component
for the text specified in the FindText property of a find
dialog component. If found, the first occurrence of the text
in Memo1 is selected. The code uses the Pos function to
compare strings, and stores the number of characters to skip
when determining the selection position in the SkipChars
variable. Because there is no handling of case, whole word,
or search direction in this algorithm, it is assumed that
the Options property of FindDialog1 is set to
[frHideMatchCase, frHideWholeWord, frHideUpDown].
} 
procedure TForm1.FindDialog1Find(Sender: TObject);
var
  I, J, PosReturn, SkipChars: Integer;
begin
  for I := 0 to Memo1.Lines.Count do
  begin
    PosReturn := Pos(FindDialog1.FindText,Memo1.Lines[I]);
    if PosReturn <> 0 then {found!}
    begin
      SkipChars := 0;
      for J := 0 to I - 1 do
        SkipChars := SkipChars + Length(Memo1.Lines[J]);
      SkipChars := SkipChars + (I*2);
      SkipChars := SkipChars + PosReturn - 1;
      Memo1.SetFocus;
      Memo1.SelStart := SkipChars;
      Memo1.SelLength := Length(FindDialog1.FindText);
      Break;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
const Path = 'country.list';
begin
  Memo1.Lines.LoadFromFile(Path);
end;

 

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