Sign in Join
Wiki copytemporary

Stringlist

Deutsch · English · Polski

String list

With a string list, a file is read completely into working memory. Stringlist is therefore suitable for files of small and medium size. Stringlist is a way to process files quickly and easily. Stringlist is ideal for processing files that are smaller than 1 megabyte. To process the file, the entire file is loaded into the main memory. Each line of the data record is read into its own array element.

Create a file

pascal
procedure SubStringlistFileCreate ();
var
   // TStringlist is part of the Classes unit
   strList : TStringList;
begin
   // Creates the string list
   strList := TStringList.Create;

   // Adds a record including a line break to the string list
   strList.Add('xyz');

   // Adds a record including a line break to the string list
   strList.Add('abcd');

   // Writes the string list to a file
   strList.SaveToFile('example.txt');

   // Releases the string list memory
   strList.Free;
end;

Append a record to an existing file

pascal
procedureSubStringlistApplyFile ();
var
   // TStringlist is part of the Classes unit
   strList : TStringList;
begin
   // Creates the string list
   strList := TStringList.Create;

   // Reads the whole file into the string list
   strList.LoadFromFile('example.txt');

   // Adds a record including a line break to the string list
   strList.Add('text');

   // Writes the string list to a file
   strList.SaveToFile('example.txt');

   // Releases the string list memory
   strList.Free;
end;

Find a record

Method 1

pascal
procedure subStringlistFileSearch ();
var
   // TStringlist is part of the Classes unit
   strList : TStringList;
   intI : Integer;
begin
   // Creates the string list
   strList := TStringList.Create;

   // Reads the whole file into the string list
   strList.LoadFromFile('example.txt');

   // Use a counting loop to find the corresponding record
   for intI := 0 to strList.Count -1 do
     begin
       // Search and change the record
       if strList [intI] = 'abcd' then 
         strList [intI] := 'dcba';
     end;

   // Writes the string list to a file
   strList.SaveToFile('example.txt');

   // Releases the string list memory
   strList.Free;
end;

Method 2

pascal
procedure subStringlistInFileSearch2 ();
var
   // TStringlist is part of the Classes unit
   strList : TStringList;
   intI : Integer;
begin
   // Creates the string list
   strList := TStringList.Create;

   // Reads the whole file into the string list
   strList.LoadFromFile('example.txt');

   // Searches for "Text", if not found, the search returns -1
   intI := strList.IndexOf ('Text');

   // If found, changes the record
   if intI <> -1 then
     strlist[intI] := 'tXET';

   // Writes the string list to a file
   strList.SaveToFile('example.txt');

   // Releases the string list memory
   strList.Free;
 end;

Sort a file alphabetically

pascal
procedure SubStringlistFileSort ();
var
  // TStringlist is part of the Classes unit
  strList : TStringList;
  intI : Integer;
 begin
   // Creates the string list
   strList := TStringList.Create;

   // Reads the whole file into the string list
   strList.LoadFromFile('example.txt');

   // Sort the string list alphabetically
   strList.Sort;

   // Writes the string list to a file
   strList.SaveToFile('example.txt');

   // Releases the string list memory 
   strList.Free;
 end;

See also


From the Free Pascal wiki, page "Stringlist", as it stood on 2020-01-15 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.