TStringList
Description
TStringsList introduces many properties and methods to:
- Add or delete strings at specified positions in the list.
- Rearrange the strings in the list.
- Access the string at a particular location.
- Read the strings from or write the strings to a file or stream.
- Associate an object with each string in the list.
- Store and retrieve strings as name-value pairs.
Properties
Свойство |
Назначение |
Count: Integer |
The number of strings in the list. |
Sorted: Boolean |
Specifies whether the strings in the list should be automatically sorted. |
Text: String |
Lists the strings in the TStringsList object as a single string with the individual strings delimited by carriage returns and line feeds. |
Methods
Метод |
Назначение |
function Add (const S: string): Integer |
Adds a new string to the list. Add returns the position of the item in the list, where the first item in the list has a value of 0. |
procedure Clear |
Deletes all the strings from the list. |
procedure Delete (Index: Integer) |
Removes the string specified by the Index parameter. |
function Find (s: string; var Index: integer): Boolean |
Locates the index for a string in a sorted list and indicates whether a string with that value already exists in the list. If the list does not contain a string that matches S, Find returns false. Only use Find with sorted lists. For unsorted lists, use the IndexOf method instead. |
function IndexOf (const S: string): Integer |
Returns the position of a string in the list. If the string does not have a match in the string list, IndexOf returns -1. |
procedure Insert (Index: Integer; const S: string) |
Inserts a string to the list at the position specified by Index. If the list is sorted, calling Insert will raise an EListError exception. Use Add with sorted lists. |
procedure LoadFromFile (const FileName: string) |
Fills the string list with the lines of text in a specified file. |
procedure Move (CurIndex, NewIndex: Integer) |
Changes the position of a string in the list. Use Move to move the string at position CurIndex so that it occupies the position NewIndex. |
procedure SaveToFile (const FileName: string) |
Saves the strings in the current object to the specified FileName file. |
procedure Sort |
Sorts the strings in the list in ascending order. |
Example
var
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Add('String 1');
sl.Add('String 2');
sl.Add('String 3');
sl.Insert(1, 'One more string'); // inserts a new string in the list, the numbering starts from zero.
sl.SaveToFile('d:\textfile.txt');
finally
sl.Free;
end;