Difference between revisions of "TStringList-TStrings Tutorial"

From Lazarus wiki
Jump to navigationJump to search
(→‎TStringList: Typos, grammar. Modified example programs so they actually compile ;))
Line 70: Line 70:
 
==File Handling==
 
==File Handling==
  
When using the TStringList you have 2 main file handling procedures.
+
When using the TStringList you have 2 file handling procedures: '''SaveToFile''' and '''LoadFromFile'''.
'''SaveToFile''' And '''LoadFromFile'''
+
SavetoFile will save all strings in the list to a file.
By Using SavetoFile it will save all strings in the list to a file.
 
 
LoadFromFile will open the file and add the file data to the list string by string.
 
LoadFromFile will open the file and add the file data to the list string by string.
  
<Delphi>var
+
<Delphi>
 +
program StrListFile;
 +
{$mode objfpc}
 +
uses
 +
Classes, SysUtils;
 +
 
 +
var
 
   Str: TStringList;
 
   Str: TStringList;
 
begin
 
begin
Line 82: Line 87:
 
   Str.Add('Hello');
 
   Str.Add('Hello');
 
   Str.SaveToFile('SomeFile.txt');
 
   Str.SaveToFile('SomeFile.txt');
end;</Delphi>
+
end.
 +
</Delphi>
  
You just opened a file,edited it and saved it right back to were it was!
+
You just opened a file, edited it and saved it right back to were it was!
  
 
==Dynamic string array comparison==
 
==Dynamic string array comparison==

Revision as of 16:17, 17 October 2011

TStringList

The TStringList (or TStrings) is much like a fancy dynamic array or Set of Strings (a set of strings is not possible in FPC). It will come in handy a lot when programming and I'm going to teach you basic TStringList usage!

<Delphi> program StrList; {$mode objfpc} uses

Classes, SysUtils;

var

 Str: TStringList;

begin

 Str := TStringList.Create; // This is needed when using this class(or most classes)
 Str.Add('Some String!');
 writeln('The stringlist now has ' + IntToStr(Str.Count) + ' string(s).');
 Readln;

end. </Delphi>

This is a simple console program that will create and add one string to a stringlist. Now here's some things you should know:

Create - Will create the string list for modifying.

Count - Is a counter for the number of strings in the List.

Add - Will allow you to add one string to the stringlist. It is a function that will return the Index of the String. This is where the counter comes in handy.

Delete - Will delete a string from the stringlist. Just know that you do not simply input the string, you have to input the index of the string. Like I said: it's like a fancy Dynamic Array.

IndexOf - Will return the index of the string in the list. If it is not found it returns -1.

Clear - Will clear the list.

Example

How about a more juicy example, Eh?

<Delphi> program StrList2; {$mode ObjFPC} uses Classes, SysUtils;

var

 Str: TStringList;
 S: String;
 Counter: Integer;

begin

 Str := TStringList.Create;
 Writeln('String List Test');
 repeat
   Writeln('Enter a string to add');
   Readln(S); 
   if (S = 'EXIT') then Halt;
   if not (S = ) then
   begin
     Counter := Str.Add(S);
     Writeln('String: ' + S + ' was Added!');
     Writeln('Index is: ' + IntToStr(Counter)); // The counter will always become the index of the last thing added
   end 
   else 
   begin
     Writeln('No data entered...');
   end;
 until (S = 'EXIT');

end. </Delphi>

File Handling

When using the TStringList you have 2 file handling procedures: SaveToFile and LoadFromFile. SavetoFile will save all strings in the list to a file. LoadFromFile will open the file and add the file data to the list string by string.

<Delphi> program StrListFile; {$mode objfpc} uses

Classes, SysUtils;

var

 Str: TStringList;

begin

 Str := TStringList.Create;
 Str.LoadFromFile('SomeFile.txt');
 Str.Add('Hello');
 Str.SaveToFile('SomeFile.txt');

end. </Delphi>

You just opened a file, edited it and saved it right back to were it was!

Dynamic string array comparison

TStringList is simply advanced object version of dynamic string array. Some methods have analogy:

Operation array of string TStringList
Variable declaration StringList: array of string; StringList: TStringList;
Initialization implicit constructor StringList := TStringList.Create
Set size  SetLength(StringList, X);  StringList.Size := X;
Get size X := Length(StringList); X := StringList.Size;
Add item SetLength(StringList, Length(StringList) + 1); StringList[Length(StringList) - 1] := X; StringList.Add(X);
Delete item for I := Index to Length(StringList) - 2 do StringList[I] := StringList[I + 1]; SetLength(StringList, Length(StringList) - 1);  StringList.Delete(Index);
Remove all items SetLength(StringList, 0); StringList.Clear;
Finalization  implicit destructor  StringList.Free;

But TStringList offer much more functionality than basic structure as dynamic array.

Keep Learning

You can learn all the different procedures, functions and property's see TStringList documentation

IF you feel i might have left something out, Modify At Will!

Hope I helped!