TStringList-TStrings Tutorial

From Lazarus wiki
Revision as of 00:32, 16 August 2010 by Cjaster (talk | contribs) (Learn how to use the TStringList)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

TStringList

The TStringList(Or TStrings) is much like a fancy dynamic array or Set of Strings(Not Possible). It will come in handy alot when programming and im going to teach you basic TStringList Usage!

<Delphi> Program StrList;

Var

Str:TStringList;

begin

 Str := TStringList.Create;//This is needed when using this class(or most classes)
 Str.Add('Some String!');
 Readln;

end; </Delphi>

This is a simple program(Console) that will create and add one string to a string list! Now heres some things you should know:

Create - Will create the string list for modifying.

Count - Is a counter for the amount of strings in the List. It is a zero based counter

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

Delete - Will delete a string from the string list. Just know that you do not simply input the string, you have to input the index of the string.(Like I said. Its like a fancy Dynamic Array)

IndexOf - Will allow you to 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 StrList;

Var

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

begin

 Str := TStringList.Create;
 Writeln('String List Test');
 Repeat
 Writeln('Input 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 Writeln('Nothing Inputed :D');
  Until(S = 'EXIT');

end; </Delphi>

Keep Learning

You can learn all the different procedures,functions and property's Here: [1]

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

Hope I helped!