RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStrings.Append Method

Adds a string to the list.

Pascal
procedure Append(const S: string);
C++
__fastcall Append(const AnsiString S);

Append is the same as the Add method, except that it does not return a value. Use Append when there is no need to know the index of the string after it has been added, or with descendants of TStrings for which the index returned is not meaningful.  

For example, the TStrings descendant used by memo objects uses an index to determine where to insert a string, but the inserted string does not necessarily end up as a single string in the list. Part of the inserted text may become part of the previous string, and part may be broken off into a subsequent string. The index returned by Add is not meaningful in this case.  

Use Append rather than Add as a parameter for a function requiring a TGetStrProc.  

C++ Examples: 

 

/*
This example uses an Open dialog box, a memo, and a button
on a form. When the user clicks the button, the Open dialog
box appears. When the user selects files in the dialog box
and chooses the OK button, the first line from each of the
files is added to the memo. Choose multiple files using the
CNTL key or the SHIFT key.
*/

#include <stdio.h>     // for FILE

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  FILE *stream;
  char FirstLine[512];
  
  OpenDialog1->Options.Clear();
  OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
  OpenDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
  OpenDialog1->FilterIndex = 2; // start the dialog showing all files 
  if (OpenDialog1->Execute())
  {
    for (int I = 0; I < OpenDialog1->Files->Count; I ++)
    {
      stream = fopen(AnsiString(OpenDialog1->Files->Strings[I]).c_str(), "r");
      if (stream)
      {
        // read the first line from the file
        fgets(FirstLine, sizeof(FirstLine), stream);
        Memo1->Lines->Append(FirstLine);
        fclose(stream);
      }
    }
  }
}

 

Delphi Examples: 

{
This example uses an Open dialog box, a memo, and a button
on a form. When the user clicks the button, the Open dialog
box appears. When the user selects files in the dialog box
and chooses the OK button, the first line from each of the
files is added to the memo. Choose multiple files using the
CNTL key or the SHIFT key.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  I: integer;
  F: TextFile;
  FirstLine: string;
begin
  OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist];
  OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
  OpenDialog1.FilterIndex := 2; { start the dialog showing all files } 
  if OpenDialog1.Execute then
    with OpenDialog1.Files do
      for I := 0 to Count - 1 do
      begin
        AssignFile(F, Strings[I]);  { next file in Files property }
        Reset(F);
        Readln(F, FirstLine);  { Read the first line out of the file }
        Memo1.Lines.Append(FirstLine);  { Add the line to the memo }
        CloseFile(F);
      end;
end;

 

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