TStringList: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | A **TStringList** is a [[Data type|datatype]] that can hold an arbitrary length list of [[String|strings]]. The strings in a TStringList are accessible as concatenated plain text or as a series of strings. Functionality is also provided for key-value pair access. | |
| 2 | 2 | ||
| 3 | 3 | Inheritance | |
| 4 | 4 | ||
| 5 | 5 | - [[TObject]] - Base [[Class|class]] of all classes. | |
| 6 | 6 | - [[TPersistent]], [[IFPObserved]] - Base class for streaming system and persistent properties - Interface implemented by an object that can be observed. | |
| 7 | 7 | - [[TStrings]] - Class to manage arrays or collections of strings | |
| 8 | 8 | - **TStringList** - Standard implementation of the TStrings class. | |
| 9 | 9 | ||
| 10 | 10 | TStringList adds sorting functionality to [[TStrings]] by adding properties `Sorted`, `Duplicates` and [[case-sensitive|CaseSensitive]] and [[Method|methods]] like `Find` to facilitate speeded search within a list. | |
| 11 | 11 | ||
| 12 | 12 | Example | |
| 13 | 13 | ||
| 14 | 14 | ``` pascal | |
| 15 | 15 | // get a value from file FILNAM filled with key=value pairs | |
| 16 | 16 | function GetValueFromFile( filnam: string, key: string ): string; | |
| 17 | 17 | var | |
| 18 | 18 | lst: TStringList; | |
| 19 | 19 | v: String; | |
| 20 | 20 | begin | |
| 21 | 21 | lst := TStringList.Create(); | |
| 22 | 22 | lst.CaseSensitive := false; | |
| 23 | 23 | lst.Duplicates := dupIgnore; // do not add duplicates | |
| 24 | 24 | lst.Sorted := true; | |
| 25 | 25 | lst.LoadFromFile( filnam ); | |
| 26 | 26 | v := lst.Values[ key ]; | |
| 27 | 27 | lst.Free(); | |
| 28 | 28 | result := v; | |
| 29 | 29 | end; | |
| 30 | 30 | ``` | |
| 31 | 31 | ||
| 32 | 32 | TStringlist also has a 'builtin' data dictionary mode, here is an example provided by forum user Remy Lebeau | |
| 33 | 33 | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 34 | + | ``` pascal | |
| 35 | + | uses | |
| 36 | + | ..., Classes; | |
| 37 | + | ||
| 38 | + | var | |
| 39 | + | SL: TStringList; | |
| 40 | + | Value: Integer; | |
| 41 | + | begin | |
| 42 | + | SL := TStringList.Create; | |
| 43 | + | try | |
| 44 | + | SL.Add('VarName=99'); | |
| 45 | + | ... | |
| 46 | + | Value := StrToInt(SL.Values['VarName']); | |
| 47 | + | ... | |
| 48 | + | finally | |
| 49 | + | SL.Free; | |
| 50 | + | end; | |
| 51 | + | end; | |
| 52 | + | ``` | |
| 51 | 53 | ||
| 52 | 54 | ## See also | |
| 53 | 55 | ||
| 54 | 56 | - [[doc:rtl/classes/tstringlist.html|TStringList doc]] | |
| 55 | 57 | - [[TStringList-TStrings Tutorial]] | |
| 56 | 58 | - [[TBufStream|example of saving a TStringList to disk faster than the SaveToFile method can]] | |
| 57 | 59 | ||
| 58 | 60 | --- | |
| 59 | 61 | *From the Free Pascal wiki, page "TStringList", as it stood on 2021-08-29 after a change by Dbannon, 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](/wiki-contact).* |
