Previous Topic

Next Topic

Inhoudsopgave

Book Index

TStringList Object

TStringList

Description

Maintains a list of strings. Use a string list object to store and manipulate a list of strings.

Details

TStrings introduces properties and methods to:

TStringList Properties

Property Property Count: Integer (R)

Indicates the number of strings in the list. Use Count when iterating over all the strings in the list, or when trying to locate the position of a string relative to the last string in the list.

Property Property Items[Index: Integer]: String (R/W)

Lists the strings, referenced by a 0-based index. Use Items to read or modify the string at a particular position. Index gives the position of the string, where 0 is the position of the first string, 1 is the position of the second string, and so on. To locate a particular string in the list, call the IndexOf method.

Property Property Text: String (R/W)

Lists the strings in the list as a single string with the individual strings delimited by carriage returns and line feeds. Use Text to get or set all the strings in the TStringList object in a single string delimited by carriage return, line feed pairs. When reading Text, the strings in the list will be separated by carriage return and (on Windows) line feed. If any of the strings in the list contain a carriage return (and line feed), the resulting value of Text will appear to contain more strings than is indicated by the Count property.

TStringList Methods

Method Method Add(S: String): Integer

Adds a new string to the list. Call Add to add the string S to the list. If the list is sorted, S is added to the appropriate position in the sort order. If the list is not sorted, S is added to the end of the list. Add returns the position of the item in the list, where the first item in the list has a value of 0.

Method Method Clear

Deletes all the strings from the list. Call clear to empty the list of strings.

Method Method Delete(Index: Integer)

Removes the string specified by the Index parameter. Call Delete to remove a single string from the list. Index gives the position of the string, where 0 is the first string, 1 is the second string, and so on.

Method Method IndexOf(S: String): Integer

Returns the position of a string in the list. Call IndexOf to obtain the position of the first occurrence of a string that matches S. A string matches S if it is identical to S.

IndexOf returns the 0-based index of the string. Thus, if S matches the first string in the list, IndexOf returns 0, if S is the second string, IndexOf returns 1, and so on. If the string does not have a match in the string list, IndexOf returns -1.

Method Method Insert(Index: Integer, S: String)

Inserts a string to the list at the position specified by Index. Call Insert to add the string S to the list at the position specified by Index. If Index is 0, the string is inserted at the beginning of the list. If Index is 1, the string is put in the second position of the list, and so on.

Method Method Sort

Sorts the strings in the list in ascending order. Call Sort to sort the strings in a list.

Method Method LoadFromFile(FileName: String)

Fills the list with the lines of text in a specified file. Call LoadFromFile to fill the list of the TStringList object from the file specified by FileName. LoadFromFile first clears any strings already in the list. Then, each line in the file, as indicated by carriage return or linefeed characters, is appended as a string in the list.

Method Method SaveToFile(FileName: String)

Saves the strings in the list to the specified file. Call SaveToFile to save the strings in the list to the file specified by FileName. Each string in the list is written to a separate line in the file.