• Wiki
  • Delphi TStringList: What Is It and How to Declare

Delphi TStringList: What Is It and How to Declare

TStrings vs. TStringList in Delphi – Understanding the Key Differences

Publish date:
Discover more of what matters to you

Working with Delphi strings can be frustrating because of unsorted lists, duplicate values, and complex file I/O operations. Developers often need to manage dynamic arrays or use basic string lists manually. This leads to inefficient code, unnecessary memory consumption, and performance bottlenecks. 

But TStringList can help. It provides an easy-to-use solution to streamline these tasks. In this article, we are exploring how TStringList can help you manage strings more effectively and how it is connected with TString. 

What are TStringList and TStrings?

TStringList and TStrings are classes in Delphi used for managing lists of strings. But they have some key differences:

TStrings

TStrings is an abstract base class that defines a common interface for string lists. It introduces abstract properties and methods for working with string collections. It has an abstract nature, which means that developers can’t create or use an instance of the class. They need to use a derived class such as TStringList.

TStringList

Delphi TStringList is a concrete implementation of the TStrings class. It inherits all its properties and methods but also adds new functionality. Furthermore, while TString defines the interface, TStringList provides the actual implementation for storing and manipulating strings. And because of its inheritance relationship, TStringList can be used wherever a TStrings parameter is expected. 

It is the recommended class for working with string lists in Delphi. 

Here’s the list of Delphi TStringList key features:

  • Capacity to hold up to 2,147,483,647 strings;
  • Sorting strings;
  • Removal of duplicate strings from sorted lists;
  • Control over case sensitivity for searching and sorting;
  • Dynamic operations like inserting and moving strings.

TStringList is useful when you need to manage a list of strings with advanced functionality. It allows developers to insert, move, and sort items. Also, they can remove duplicates, load and save strings from files, and associate objects with strings using the Objects property. Additionally, it helps maintain backward compatibility with older Delphi versions. 

This class is used for various purposes, such as populating UI controls like TListBox, TComboBox, and TMemo. It parses delimited strings, temporarily stores string data, and performs file I/O operations. 

How to Use Gestures in Embarcadero Delphi FMX App?
Learn in our article.
Read More

Declaration

By declaring TStrings and instantiating TStringList, developers ensure flexibility in their code. Also, they maintain access to all the methods implemented in TStringList.

12345678
type
TStrings = class(TPersistent)
// Abstract methods and properties
End;
TStringList = class(TStrings)
// Concrete implementation with additional methods and properties
end;

Function Commands and Their Descriptions

Here’s the list of commonly used methods and their descriptions:

Add – adding a new string to the end of the list and returning the index of the added string. 

Declaration: function Add(const S: string): Integer;

Insert – inserting a string at the specified index in the list.

Declaration: procedure Insert(Index: Integer; const S: string);

Delete – deletes the string at the specified index. 

Declaration: procedure Delete(Index: Integer);

IndexOf – Returns the index of the first occurrence of the specified string. Returns -1 if the string is not found. 

Declaration: function IndexOf(const S: string): Integer;

Clear – removes all strings from the list.

Declaration: procedure Clear;

LoadFromFile – loads strings from a file into the list.

Declaration: procedure LoadFromFile(const FileName: string);

SaveToFile – saves the strings in the list to a file.

Declaration: procedure SaveToFile(const FileName: string);

Sort – sorts the strings in the list in ascending order.

Declaration: procedure Sort;

Code Example

Below is the code example that shows the use of TStringList to add, sort, and save strings to a file.

1234567891011121314151617181920212223242526272829303132333435363738394041424344
program TStringListExample;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
var
StringList: TStringList;
I: Integer;
begin
// Create a TStringList object
StringList := TStringList.Create;
try
// Add some strings to the list
StringList.Add('Delphi');
StringList.Add('is');
StringList.Add('a');
StringList.Add('powerful');
StringList.Add('programming');
StringList.Add('language');
// Display the strings before sorting
Writeln('Before sorting:');
for I := 0 to StringList.Count – 1 do
Writeln(StringList[I]);
// Sort the strings
StringList.Sort;
// Display the strings after sorting
Writeln('After sorting:');
for i := 0 to StringList.Count – 1 do
Writeln(StringList[I]);
// Save the sorted strings to a file
StringList.SaveToFile('sorted_strings.txt');
Writeln('Strings saved to sorted_strings.txt');
finally
// Free the TStringList object
StringList.Free;
end;
end.

The TStringList object is created using TStringList.Create. Then, we added strings to the list using the Add method. The Sort method is used to sort the strings in ascending order. To save the sorted strings, we used the SaveToFile method, and the strings went to the file named sorted_strings.txt. And after that, we released memory by the Free method.  

Conclusion

Understanding the difference between TStrings and TStringList helps developers write cleaner and more efficient Delphi applications. TStringList allows them to simplify string operations and maintain flexibility and performance.

Explore Softacom Delphi Development Services

Subscribe to our newsletter and get amazing content right in your inbox.

This field is required
This field is required Invalid email address
By submitting data, I agree to the Privacy Policy

Thank you for subscribing!
See you soon... in your inbox!

confirm your subscription, make sure to check your promotions/spam folder

Subscribe to our newsletter and get amazing content right in your inbox.

You can unsubscribe from the newsletter at any time

This field is required
This field is required Invalid email address

You're almost there...

A confirmation was sent to your email

confirm your subscription, make sure to check
your promotions/spam folder