Difference between revisions of "TStringList-TStrings Tutorial"

From Lazarus wiki
Jump to navigationJump to search
(category and style)
Line 3: Line 3:
 
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 I'm going to teach you basic TStringList Usage!
 
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 I'm going to teach you basic TStringList Usage!
  
<Delphi>
+
<Delphi>program StrList;
Program StrList;
 
 
 
Var
 
Str:TStringList;
 
  
 +
var
 +
  Str: TStringList;
 
begin
 
begin
   Str := TStringList.Create;//This is needed when using this class(or most classes)
+
   Str := TStringList.Create; // This is needed when using this class(or most classes)
 
   Str.Add('Some String!');
 
   Str.Add('Some String!');
 
   Readln;
 
   Readln;
end;
+
end;</Delphi>
</Delphi>
 
  
 
This is a simple program(Console) that will create and add one string to a string list!
 
This is a simple program(Console) that will create and add one string to a string list!
Line 35: Line 32:
 
How about a more juicy example, Eh?
 
How about a more juicy example, Eh?
  
<Delphi>
+
<Delphi>program StrList;
 
 
Program StrList;
 
 
 
Var
 
Str:TStringList;
 
S:String;
 
Counter:Integer;
 
  
 +
var
 +
  Str: TStringList;
 +
  S: String;
 +
  Counter: Integer;
 
begin
 
begin
 
   Str := TStringList.Create;
 
   Str := TStringList.Create;
 
   Writeln('String List Test');
 
   Writeln('String List Test');
   Repeat
+
   repeat
  Writeln('Input a string to add');
+
    Writeln('Input a string to add');
  Readln(S);  
+
    Readln(S);  
  IF(S = 'EXIT')Then Halt;
+
    if (S = 'EXIT') then Halt;
  IF NOT(S = '')Then
+
    if not (S = '') then
  begin
+
    begin
    Counter := Str.Add(S);
+
      Counter := Str.Add(S);
    Writeln('String: '+S'+ was Added!');
+
      Writeln('String: ' + S + ' was Added!');
    Writeln('Index is: '+InttoStr(Counter);//The counter will always become the index of the last thing added
+
      Writeln('Index is: ' + IntToStr(Counter); // The counter will always become the index of the last thing added
  end Else Writeln('Nothing Inputed :D');
+
    end rlse Writeln('Nothing Inputed :D');
  Until(S = 'EXIT');
+
  until (S = 'EXIT');
end;
+
end;</Delphi>
</Delphi>
 
  
 
==File Handling==
 
==File Handling==
Line 68: Line 61:
 
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>
+
<Delphi>var
Var
+
  Str: TStringList;
Str:TStringList;
 
 
begin
 
begin
 
   Str := TStringList.Create;
 
   Str := TStringList.Create;
Line 76: Line 68:
 
   Str.Add('Hello');
 
   Str.Add('Hello');
 
   Str.SaveToFile('SomeFile.txt');
 
   Str.SaveToFile('SomeFile.txt');
end;
+
end;</Delphi>
</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!
Line 87: Line 78:
  
 
Hope I helped!
 
Hope I helped!
 +
 +
[[Category:Tutorials]]

Revision as of 22:27, 9 September 2010

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 I'm 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 rlse Writeln('Nothing Inputed :D');
 until (S = 'EXIT');

end;</Delphi>

File Handling

When using the TStringList you have 2 main file handling procedures. SaveToFile And LoadFromFile 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.

<Delphi>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!

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!