Sign in Join
Wiki copytemporary

Stringlist: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Stringlist

OldNewLine
1+## String list
2+
3+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.
4+
5+## Create a file
6+
7+``` pascal
8+procedure SubStringlistFileCreate ();
9+var
10+ // TStringlist is part of the Classes unit
11+ strList : TStringList;
12+begin
13+ // Creates the string list
14+ strList := TStringList.Create;
15+
16+ // Adds a record including a line break to the string list
17+ strList.Add('xyz');
18+
19+ // Adds a record including a line break to the string list
20+ strList.Add('abcd');
21+
22+ // Writes the string list to a file
23+ strList.SaveToFile('example.txt');
24+
25+ // Releases the string list memory
26+ strList.Free;
27+end;
28+```
29+
30+## Append a record to an existing file
31+
32+``` pascal
33+procedureSubStringlistApplyFile ();
34+var
35+ // TStringlist is part of the Classes unit
36+ strList : TStringList;
37+begin
38+ // Creates the string list
39+ strList := TStringList.Create;
40+
41+ // Reads the whole file into the string list
42+ strList.LoadFromFile('example.txt');
43+
44+ // Adds a record including a line break to the string list
45+ strList.Add('text');
46+
47+ // Writes the string list to a file
48+ strList.SaveToFile('example.txt');
49+
50+ // Releases the string list memory
51+ strList.Free;
52+end;
53+```
54+
55+## Find a record
56+
57+### Method 1
58+
59+``` pascal
60+procedure subStringlistFileSearch ();
61+var
62+ // TStringlist is part of the Classes unit
63+ strList : TStringList;
64+ intI : Integer;
65+begin
66+ // Creates the string list
67+ strList := TStringList.Create;
68+
69+ // Reads the whole file into the string list
70+ strList.LoadFromFile('example.txt');
71+
72+ // Use a counting loop to find the corresponding record
73+ for intI := 0 to strList.Count -1 do
74+ begin
75+ // Search and change the record
76+ if strList [intI] = 'abcd' then
77+ strList [intI] := 'dcba';
78+ end;
79+
80+ // Writes the string list to a file
81+ strList.SaveToFile('example.txt');
82+
83+ // Releases the string list memory
84+ strList.Free;
85+end;
86+```
87+
88+### Method 2
89+
90+``` pascal
91+procedure subStringlistInFileSearch2 ();
92+var
93+ // TStringlist is part of the Classes unit
94+ strList : TStringList;
95+ intI : Integer;
96+begin
97+ // Creates the string list
98+ strList := TStringList.Create;
99+
100+ // Reads the whole file into the string list
101+ strList.LoadFromFile('example.txt');
102+
103+ // Searches for "Text", if not found, the search returns -1
104+ intI := strList.IndexOf ('Text');
105+
106+ // If found, changes the record
107+ if intI <> -1 then
108+ strlist[intI] := 'tXET';
109+
110+ // Writes the string list to a file
111+ strList.SaveToFile('example.txt');
112+
113+ // Releases the string list memory
114+ strList.Free;
115+ end;
116+```
117+
118+## Sort a file alphabetically
119+
120+``` pascal
121+procedure SubStringlistFileSort ();
122+var
123+ // TStringlist is part of the Classes unit
124+ strList : TStringList;
125+ intI : Integer;
126+ begin
127+ // Creates the string list
128+ strList := TStringList.Create;
129+
130+ // Reads the whole file into the string list
131+ strList.LoadFromFile('example.txt');
132+
133+ // Sort the string list alphabetically
134+ strList.Sort;
135+
136+ // Writes the string list to a file
137+ strList.SaveToFile('example.txt');
138+
139+ // Releases the string list memory
140+ strList.Free;
141+ end;
142+```
143+
144+## See also
145+
146+- [[File types]]
147+
148+---
149+*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](/wiki-contact).*
150+Category: Code Snippets
151+Category: FPC
152+Category: Pascal

Back to history