From Clomosy Docs

No edit summary
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
TclStringList is a convenient class used to store and manage an array of strings, providing a list-like structure. TclStringList enables the ability to add, remove, sort, search, and perform other operations on string elements within the list.
TclStringList is a convenient class used to store and manage an array of strings, providing a list-like structure. TclStringList enables the ability to add, remove, sort, search, and perform other operations on string elements within the list.<br>


To utilize TclStringList, a variable of type TclStringList should be defined.
The features and usage are provided in the table below.<br>


  '''Var'''
<div class="table-responsive">
  List:TclStringList;
{| class="wikitable" style="border: 2px solid #c3d7e0"
! style="background-color: #c3d7e0"| Feature !!style="background-color: #c3d7e0"| Use of !!style="background-color: #c3d7e0"| Definition
|-
|TclStringList || List1:TclStringList; || A variable belonging to the TclStringList class is created.
|-
|StringListNew || List1 = Clomosy.StringListNew; || When an object of the TclStringList class is defined, the object is then created.
|-
|StringListItemString || Str1 = Clomosy.StringListItemString(List1,0); //Str1 : String || The property is used to access items in the list. The first parameter represents the created object, while the second parameter indicates the index number to be accessed.
|-
|Add ||List1.Add('Test 1'); || It is used to add a new item to the end of the list.
|-
|Insert || List1.Insert(1, 'Test'); || It is used to insert a new item at a specific position. The first parameter specifies the index number where the item will be added, and the second parameter specifies the string to be inserted.
|-
|Delete || List1.Delete(1); || It is used to remove an item from the list at a specific index.
|-
|Text || List1.Text || It is a property that returns a combined version of all the items in the object. This property concatenates the items in the list into a single text string.
|-
|Clear || List1.Clear; || The clear property is used to clear all the items of the list.
|-
|Free ||List1.Free; || The specified object and its associated resources (memory, files, etc.) are cleared from the system, thereby conserving memory and preventing memory leaks.
|-
|Capacity || List1.Capacity = 5; || Determines or returns the capacity of the list. Capacity represents the size of the allocated memory space for the list. The capacity is dynamically adjusted automatically during operations such as adding or removing elements.
|-
|Count ||ItemCount = List1.Count; //ItemCount : Integer || It returns the number of items in the list.
|-
|Sorted ||List1.Sorted = True; || It returns or sets a Boolean value indicating whether the items in the list are sorted.
|-
|StrictDelimiter || List1.StrictDelimiter = True; || This property specifies whether only the set delimiter character should be used for splitting the string. If StrictDelimiter is True, it ensures that only the specified delimiter character (like a comma) is used to separate items, ignoring any other whitespace or special characters.
|-
|Delimiter || List1.Delimiter = ','; || This property defines the character that separates items in the string. For instance, if you set Delimiter = ',';, the TclStringList will treat each comma in the string as a separator between items.
|-
|DelimitedText ||List1.DelimitedText = str; //str = 'hello,world,test'; || This property is used to both get and set the list items as a single string, using the Delimiter character to split or join items. When you assign a string to DelimitedText, TclStringList will use the specified Delimiter (and respect StrictDelimiter settings) to split the string into individual list items.
|-
|IndexOf || Index = List1.IndexOf('Test'); //Index: Integer; || It is used to find the index of a specific item in the list.
|-
|Exchange || List1.Exchange(0,2); || The Exchange feature is used to swap two items.
|-
|Sort || List1.Sort; || The sort property is used to sort the items of the list alphabetically.
|-
|LoadFromFile || List1.LoadFromFile(Clomosy.AppFilesPath+'note.txt',0); || To load text data from a specified file into a TclStringList object, the 'LoadFromFile' method is used. The purpose of this method is to fetch text data from a file into the program. The data loaded into the TclStringList can be read and processed line by line.
|-
|SaveToFile || List1.SaveToFile(clPathCombine('File1.Txt',Clomosy.AppFilesPath),0); || It is a class used to hold and manage lines of text. The SaveToFile method is used to save these lines of text to a file. When you call the SaveToFile method, all the lines of text in the TclStringList are written to the specified file.
|}
</div>


Then the list should be created in the application.
List := Clomosy.StringListNew;


The StringListItemString(ListVariable,index) property of the Clomosy library is used to provide access to the items in the list.
Str:=Clomosy.StringListItemString(List,0);


Here are some important properties of the TclStringList class:
<h2> Example : Usage of Capacity </h2>


=Capacity=
In the example, we set the capacity of the list to 10 using the Capacity property of the TclStringList class. Then, by adding elements that exceed the capacity, we create a situation where the capacity is exceeded. In this case, the list automatically expands, and we check the new capacity.<br>
Determines or returns the capacity of the list. Capacity represents the size of the allocated memory space for the list. The capacity is dynamically adjusted automatically during operations such as adding or removing elements.


''Example:''<br>
<pre>
:'''Base Syntax'''
  void TclStringListCapacityOrnegi;
  '''procedure''' TclStringListCapacityOrnegi;
  var
  '''var'''
   List: TclStringList;
   List: TclStringList;
  '''begin'''
  {
   List := Clomosy.StringListNew; //A new list has been created
   List = Clomosy.StringListNew; //A new list has been created
   try
   try
     List.Capacity := 5; // Set capacity to 10  
     List.Capacity = 5; // Set capacity to 10  
   
   
     // Check capacity
     // Check capacity
Line 43: Line 80:
   finally
   finally
     List.Free;
     List.Free;
   end;
   }
  '''end;'''
  }
   
   
  '''begin'''
  {
   TclStringListCapacityOrnegi;
   TclStringListCapacityOrnegi;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
<b>Output:</b><br>
  void TclStringListCapacityOrnegi;
<div class="alert alert-success" role="alert" data-bs-theme="light">
  var
Capacity: 5<br>
    List: TclStringList;
New capacity: 9
  {
</div>
    List = Clomosy.StringListNew; //A new list has been created
    try
    {
      List.Capacity = 5; // Set capacity to 10
 
      // Check capacity
      ShowMessage('Capacity: '+IntToStr(List.Capacity));
 
      // Let's exceed capacity by adding more items
      List.Add('Item 1');
      List.Add('Item 2');
      List.Add('Item 3');
      List.Add('Item 4');
      List.Add('Item 5');
      List.Add('Item 6');
      List.Add('Item 7');
 
      // Check new capacity
      ShowMessage('New capacity: '+ IntToStr(List.Capacity));
    }
    finally
    {
      List.Free;
    }
  }
 
  {
    TclStringListCapacityOrnegi;
  }
 
''Output:''<br>
Capacity: 5
New capacity: 9
 
In the above example, we set the capacity of the list to 10 using the Capacity property of the TclStringList class. Then, by adding elements that exceed the capacity, we create a situation where the capacity is exceeded. In this case, the list automatically expands, and we check the new capacity.


=Count=
<h2>Example : Usage of Count</h2><br>
The Count property of the TclStringList class returns the number of items in the list. Here is an example:<br>
 
''Example:''<br>
:'''Base Syntax'''


<pre>
  var
  var
   MyList: TclStringList;
   MyList: TclStringList;
   ItemCount: Integer;
   ItemCount: Integer;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
   
   
   // Add items
   // Add items
Line 109: Line 109:
   
   
   // Get item count
   // Get item count
   ItemCount := MyList.Count;
   ItemCount = MyList.Count;
    
    
   // Show result
   // Show result
Line 115: Line 115:
    
    
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
<b>Output:</b><br>
  var
<div class="alert alert-success" role="alert" data-bs-theme="light">
    MyList: TclStringList;
There are 3 items in the list.
    ItemCount: Integer;
</div>
  {
    MyList = Clomosy.StringListNew;
 
    // Add items
    MyList.Add('Apple');
    MyList.Add('Banana');
    MyList.Add('Orange');
 
    // Get item count
    ItemCount = MyList.Count;
   
    // Show result
    ShowMessage('There are ' + IntToStr(ItemCount) + ' items in the list.');
   
    MyList.Free;
  }


<h2>Example : Usage of Sorted</h2><br>


''Output:''<br>
<pre>
  There are 3 items in the list.
  var
 
=Sorted=
 
The Sorted property of the TclStringList class returns or sets a Boolean value that indicates whether the elements of the list are ordered.
 
''Example:''<br>
:'''Base Syntax'''
'''var'''
   MyList: TclStringList;
   MyList: TclStringList;
   i: Integer;
   i: Integer;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   // Add unordered items
   // Add unordered items
Line 161: Line 139:
   // Show unordered list items
   // Show unordered list items
   ShowMessage('Unsorted List:');
   ShowMessage('Unsorted List:');
   for i := 0 to MyList.Count - 1 do
   for (i = 0 to MyList.Count - 1)
     ShowMessage(Clomosy.StringListItemString(MyList,i));
     ShowMessage(Clomosy.StringListItemString(MyList,i));
    
    
   // Sort items
   // Sort items
   MyList.Sorted := True;
   MyList.Sorted = True;
    
    
   // Show sorted list items
   // Show sorted list items
   ShowMessage('Sorted List:');
   ShowMessage('Sorted List:');
   for i := 0 to MyList.Count - 1 do
   for (i = 0 to MyList.Count - 1)
     ShowMessage(Clomosy.StringListItemString(MyList,i));
     ShowMessage(Clomosy.StringListItemString(MyList,i));
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
 
</pre>
:'''TRObject Syntax'''
  var
    MyList: TclStringList;
    i: Integer;
  {
    MyList = Clomosy.StringListNew;
   
    // Add unordered items
    MyList.Add('Orange');
    MyList.Add('Banana');
    MyList.Add('Apple');
   
    // Show unordered list items
    ShowMessage('Unsorted List:');
    for (i = 0 to MyList.Count - 1)
      ShowMessage(Clomosy.StringListItemString(MyList,i));
   
    // Sort items
    MyList.Sorted = True;
   
    // Show sorted list items
    ShowMessage('Sorted List:');
    for (i = 0 to MyList.Count - 1)
      ShowMessage(Clomosy.StringListItemString(MyList,i));
    MyList.Free;
  }


''Output:''<br>
<b>Output:</b><br>
Unsorted List:
<div class="alert alert-success" role="alert" data-bs-theme="light">
Orange
Unsorted List:<br>
Banana
Orange<br>
Apple
Banana<br>
Sorted list:
Apple<br>
Apple
Sorted list:<br>
Banana
Apple<br>
Orange
Banana<br>
Orange
</div>


=Delimiter and DelimitedText=
<h2>Example : Usage of String Splitting</h2><br>
Sets or returns the separator character. Specifies the character used to separate the string when using the DelimitedText property.


''Example:''
<pre>
:'''Base Syntax'''
  var
  var
   list:TclStringList;
   list:TclStringList;
   str:String;
   str:String;
  begin
  {
   str := 'hello,world,test';
   str = 'hello,world,test';
   list := Clomosy.StringListNew;
   list = Clomosy.StringListNew;
    
    
   List.StrictDelimiter :=True;
   List.StrictDelimiter = True;
   List.Delimiter := ',';
   List.Delimiter = ',';
   List.DelimitedText := str;  
   List.DelimitedText = str;  
   Str:=Clomosy.StringListItemString(List,0);//Returns element 0
   Str= Clomosy.StringListItemString(List,0);//Returns element 0
   ShowMessage(str);
   ShowMessage(str);
   ShowMessage(List.Count);
   ShowMessage(List.Count);
  end;
  }
</pre>


:'''TRObject Syntax'''
<h2>Example : Usage of Add</h2><br>
  var
  list:TclStringList;
  str:String;
  {
    str = 'hello,world,test';
    list = Clomosy.StringListNew;
   
    List.StrictDelimiter = True;
    List.Delimiter = ',';
    List.DelimitedText = str;
    Str= Clomosy.StringListItemString(List,0);//Returns element 0
    ShowMessage(str);
    ShowMessage(List.Count);
  }


=Add=
<pre>
The Add property of the TclStringList class is used to add a new item to the end of the list.
  var
 
''Example:''
:'''Base Syntax'''
  '''var'''
   MyList: TclStringList;
   MyList: TclStringList;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   MyList.Add('Blue');
   MyList.Add('Blue');
Line 264: Line 199:
   ShowMessage('Items: ' + MyList.Text);
   ShowMessage('Items: ' + MyList.Text);
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
<b>Output:</b><br>
<div class="alert alert-success" role="alert" data-bs-theme="light">
Items: Blue<br>
Red<br>
Yellow<br>
Purple
</div>


  var
<h2>Example : Usage of Insert</h2><br>
    MyList: TclStringList;
<pre>
  {
  var
    MyList = Clomosy.StringListNew;
   
    MyList.Add('Blue');
    MyList.Add('Red');
    MyList.Add('Yellow');
    MyList.Add('Purple');
 
    ShowMessage('Items: ' + MyList.Text);
    MyList.Free;
  }
 
=Insert=
The Insert property of the TclStringList class is used to insert a new item at a specific location.
 
''Example:''
:'''Base Syntax'''
  '''var'''
   MyList: TclStringList;
   MyList: TclStringList;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   MyList.Add('Apple');
   MyList.Add('Apple');
Line 302: Line 227:
    
    
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
As a result, the contents of the list are displayed using the ShowMessage function.<br>
  var
    MyList: TclStringList;
  {
    MyList = Clomosy.StringListNew;
   
    MyList.Add('Apple');
    MyList.Add('Banana');
    MyList.Add('Orange');
   
    // Add "Grape" to Index 1
    MyList.Insert(1, 'Grape');
   
    ShowMessage('Items: ' + MyList.Text);
   
    MyList.Free;
  }


<b>Output:</b><br>
<div class="alert alert-success" role="alert" data-bs-theme="light">
Items: Apple Grape Banana Orange
</div>


As a result, the contents of the list are displayed using the ShowMessage function. You will get a message like "Items: Apple Grape Banana Orange" as output.
<h2>Example : Usage of Delete</h2><br>
 
<pre>
=Delete=
  var
The Delete property of the TclStringList class is used to delete the item at a particular index from the list.
 
''Example:''
:'''Base Syntax'''
  '''var'''
   MyList: TclStringList;
   MyList: TclStringList;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   MyList.Add('Apple');
   MyList.Add('Apple');
Line 345: Line 254:
    
    
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
<b>Output:</b><br>
<div class="alert alert-success" role="alert" data-bs-theme="light">
Items: Apple<br>
Orange
</div>


  var
<h2>Example : Usage of Clear</h2><br>
    MyList: TclStringList;
<pre>
  {
  var
    MyList = Clomosy.StringListNew;
   
    MyList.Add('Apple');
    MyList.Add('Banana');
    MyList.Add('Orange');
   
    // Delete "Grape" to Index 1
    MyList.Delete(1);
   
    ShowMessage('Items: ' + MyList.Text);
   
    MyList.Free;
  }
 
=Clear=
The clear property is used to clear all the items of the list.
 
''Example:''
:'''Base Syntax'''
  '''var'''
   MyList: TclStringList;
   MyList: TclStringList;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   MyList.Add('Apple');
   MyList.Add('Apple');
Line 387: Line 281:
    
    
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
As a result, the updated content of the list is displayed using the ShowMessage function. You will get a message like "Items:" (empty) as output because all items have been deleted.<br>


  var
    MyList: TclStringList;
  {
    MyList = Clomosy.StringListNew;
   
    MyList.Add('Apple');
    MyList.Add('Banana');
    MyList.Add('Orange');
    ShowMessage('Items: ' + MyList.Text);
   
    // Clear "Grape" to Index 1
    MyList.Clear;
   
    ShowMessage('Items: ' + MyList.Text);
   
    MyList.Free;
  }


As a result, the updated content of the list is displayed using the ShowMessage function. You will get a message like "Items:" (empty) as output because all items have been deleted.
<h2>Example : Usage of IndexOf</h2><br>
 
<pre>
=IndexOf=
  var
 
The IndexOf property of the TclStringList class is used to find the index of a particular item in the list.
 
''Example:''
:'''Base Syntax'''
  '''var'''
   MyList: TclStringList;
   MyList: TclStringList;
   Index: Integer;
   Index: Integer;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   MyList.Add('Apple');
   MyList.Add('Apple');
Line 427: Line 299:
   MyList.Add('Orange');
   MyList.Add('Orange');
    
    
   Index := MyList.IndexOf('Banana');
   Index = MyList.IndexOf('Banana');
    
    
   ShowMessage('Indeks: ' + IntToStr(Index));
   ShowMessage('Indeks: ' + IntToStr(Index));
    
    
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
<b>Output:</b><br>
<div class="alert alert-success" role="alert" data-bs-theme="light">
Indeks: 1
</div>


  var
<h2>Example : Usage of Exchange</h2><br>
    MyList: TclStringList;
<pre>
    Index: Integer;
  var
  {
    MyList = Clomosy.StringListNew;
   
    MyList.Add('Apple');
    MyList.Add('Banana');
    MyList.Add('Orange');
   
    Index = MyList.IndexOf('Banana');
   
    ShowMessage('Indeks: ' + IntToStr(Index));
   
    MyList.Free;
  }
 
=Exchange=
The Exchange feature is used to swap two items.
 
''Example:''
:'''Base Syntax'''
  '''var'''
   MyList: TclStringList;
   MyList: TclStringList;
   Index: Integer;
   Index: Integer;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   MyList.Add('Apple');
   MyList.Add('Apple');
Line 473: Line 329:
    
    
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>


:'''TRObject Syntax'''
<b>Output:</b><br>
  var
<div class="alert alert-success" role="alert" data-bs-theme="light">
    MyList: TclStringList;
Items: Orange<br>
    Index: Integer;
Banana<br>
  {
Apple
    MyList = Clomosy.StringListNew;
</div>
 
    MyList.Add('Apple');
    MyList.Add('Banana');
    MyList.Add('Orange');
   
    MyList.Exchange(0,2);
   
    ShowMessage('Items: ' + MyList.Text);
   
    MyList.Free;
  }


 
<h2>Example : Usage of Sort</h2><br>
As a result, the contents of items that have been updated using the ShowMessage function are displayed. You will get a message like "Items: Orange Banana Apple" as output.
<pre>
 
  var
=Sort=
The sort property is used to sort the items of the list alphabetically.
 
''Example:''
:'''Base Syntax'''
  '''var'''
   MyList: TclStringList;
   MyList: TclStringList;
   Index: Integer;
   Index: Integer;
  '''begin'''
  {
   MyList := Clomosy.StringListNew;
   MyList = Clomosy.StringListNew;
    
    
   MyList.Add('Orange');
   MyList.Add('Orange');
Line 517: Line 357:
   ShowMessage('Items: ' + MyList.Text);
   ShowMessage('Items: ' + MyList.Text);
   MyList.Free;
   MyList.Free;
  '''end;'''
  }
</pre>
 
<b>Output:</b><br>
<div class="alert alert-success" role="alert" data-bs-theme="light">
Items: Apple<br>
Banana<br>
Orange
</div>
 
<h2>Example : Install from File / LoadFromFile</h2><br>
<pre>
var
  strList:TclStringList;
  clQueryLoadFile : TclJsonQuery;
  path:String;
{
  strList = Clomosy.StringListNew;
  path = Clomosy.AppFilesPath+'note.txt';
  //ShowMessage(path);
  strList.LoadFromFile(path,0);
  ShowMessage(strList.Text); // The data in the file is displayed by putting it into a String List.
  clQueryLoadFile = Clomosy.ClDataSetFromJSON(strList.Text);
  ShowMessage(clQueryLoadFile.getJSONString);
}
</pre>
 


:'''TRObject Syntax'''
<h2>Example : Writing to a File / SaveToFile</h2><br>
Using the TclStringList class, lines of text are added to a file and then read from the file and displayed on the screen. First, a TclStringList object named StringList1 and a file path variable named FileStr are defined. The showFile procedure loads the lines from the file into the StringList1 object if the specified file exists, and then displays each line on the screen. In the main program block, the StringList1 object is created, and the FileStr variable is set to point to the MyFile.Txt file located in the application files path. Three lines of text are added to the StringList1 object, and these lines are saved to the file. Finally, the showFile procedure is called to display the lines from the file on the screen, and the StringList1 object is freed.<br>


<pre>
   var
   var
     MyList: TclStringList;
     StringList1: TclStringList;
     Index: Integer;
     FileStr:String;
    i: Integer;
  void showFile;
   {
   {
     MyList = Clomosy.StringListNew;
     If (clFileExists(FileStr,''))
   
      StringList1.LoadFromFile(FileStr,0);
    MyList.Add('Orange');
     
    MyList.Add('Banana');
     for (i = 0 to StringList1.Count - 1)
    MyList.Add('Apple');
      ShowMessage(Clomosy.StringListItemString(StringList1,i));
   
     ShowMessage('Items: ' + MyList.Text);
   
    MyList.Sort;
   
    ShowMessage('Items: ' + MyList.Text);
    MyList.Free;
   }
   }
 
  {
    StringList1 = Clomosy.StringListNew;
    FileStr = clPathCombine('MyFile.Txt',Clomosy.AppFilesPath);
    try
      // Adding rows to TclStringList
      StringList1.Add('This is line one.');
      StringList1.Add('This is line two.');
      StringList1.Add('This is line three.');
 
      // Saving TclStringList to a file
      StringList1.SaveToFile(FileStr,0);
      showFile;
    finally
      StringList1.Free;
    }
  }
</pre>


As a result, the content of items sorted using the ShowMessage function is displayed. You will get a message like "Items: Apple Banana Orange" as output.
<h2> See Also </h2>
 
* [[Data_Types#Special_Data_Types | Special Data Types]]
=Install from File=
{{#seo:|title=TclStringList Using in Clomosy - Clomosy Docs}}
To load text data from a specified file into a TclStringList object, the 'LoadFromFile' method is used. The purpose of this method is to fetch text data from a file into the program. The data loaded into the TclStringList can be read and processed line by line.
{{#seo:|description=Learn about TclStringList on Clomosy. Understand how to handle and process lists of strings for effective data management.}}
 
''Example:''
var
  strList:TclStringList;
  clQueryLoadFile : TclJsonQuery;
  path:String;
begin
  strList:=Clomosy.StringListNew;
  path:=Clomosy.AppFilesPath+'note.txt';
  //ShowMessage(path);
  strList.LoadFromFile(path,0);
  ShowMessage(strList.Text); // The data in the file is displayed by putting it into a String List.
  clQueryLoadFile:=Clomosy.ClDataSetFromJSON(strList.Text);
  ShowMessage(clQueryLoadFile.getJSONString);
end;

Latest revision as of 13:32, 24 December 2024

TclStringList is a convenient class used to store and manage an array of strings, providing a list-like structure. TclStringList enables the ability to add, remove, sort, search, and perform other operations on string elements within the list.

The features and usage are provided in the table below.

Feature Use of Definition
TclStringList List1:TclStringList; A variable belonging to the TclStringList class is created.
StringListNew List1 = Clomosy.StringListNew; When an object of the TclStringList class is defined, the object is then created.
StringListItemString Str1 = Clomosy.StringListItemString(List1,0); //Str1 : String The property is used to access items in the list. The first parameter represents the created object, while the second parameter indicates the index number to be accessed.
Add List1.Add('Test 1'); It is used to add a new item to the end of the list.
Insert List1.Insert(1, 'Test'); It is used to insert a new item at a specific position. The first parameter specifies the index number where the item will be added, and the second parameter specifies the string to be inserted.
Delete List1.Delete(1); It is used to remove an item from the list at a specific index.
Text List1.Text It is a property that returns a combined version of all the items in the object. This property concatenates the items in the list into a single text string.
Clear List1.Clear; The clear property is used to clear all the items of the list.
Free List1.Free; The specified object and its associated resources (memory, files, etc.) are cleared from the system, thereby conserving memory and preventing memory leaks.
Capacity List1.Capacity = 5; Determines or returns the capacity of the list. Capacity represents the size of the allocated memory space for the list. The capacity is dynamically adjusted automatically during operations such as adding or removing elements.
Count ItemCount = List1.Count; //ItemCount : Integer It returns the number of items in the list.
Sorted List1.Sorted = True; It returns or sets a Boolean value indicating whether the items in the list are sorted.
StrictDelimiter List1.StrictDelimiter = True; This property specifies whether only the set delimiter character should be used for splitting the string. If StrictDelimiter is True, it ensures that only the specified delimiter character (like a comma) is used to separate items, ignoring any other whitespace or special characters.
Delimiter List1.Delimiter = ','; This property defines the character that separates items in the string. For instance, if you set Delimiter = ',';, the TclStringList will treat each comma in the string as a separator between items.
DelimitedText List1.DelimitedText = str; //str = 'hello,world,test'; This property is used to both get and set the list items as a single string, using the Delimiter character to split or join items. When you assign a string to DelimitedText, TclStringList will use the specified Delimiter (and respect StrictDelimiter settings) to split the string into individual list items.
IndexOf Index = List1.IndexOf('Test'); //Index: Integer; It is used to find the index of a specific item in the list.
Exchange List1.Exchange(0,2); The Exchange feature is used to swap two items.
Sort List1.Sort; The sort property is used to sort the items of the list alphabetically.
LoadFromFile List1.LoadFromFile(Clomosy.AppFilesPath+'note.txt',0); To load text data from a specified file into a TclStringList object, the 'LoadFromFile' method is used. The purpose of this method is to fetch text data from a file into the program. The data loaded into the TclStringList can be read and processed line by line.
SaveToFile List1.SaveToFile(clPathCombine('File1.Txt',Clomosy.AppFilesPath),0); It is a class used to hold and manage lines of text. The SaveToFile method is used to save these lines of text to a file. When you call the SaveToFile method, all the lines of text in the TclStringList are written to the specified file.


Example : Usage of Capacity

In the example, we set the capacity of the list to 10 using the Capacity property of the TclStringList class. Then, by adding elements that exceed the capacity, we create a situation where the capacity is exceeded. In this case, the list automatically expands, and we check the new capacity.

 void TclStringListCapacityOrnegi;
 var
   List: TclStringList;
 {
   List = Clomosy.StringListNew; //A new list has been created
   try
     List.Capacity = 5; // Set capacity to 10 
 
     // Check capacity
     ShowMessage('Capacity: '+IntToStr(List.Capacity)); 
 
     // Let's exceed capacity by adding more items
     List.Add('Item 1');
     List.Add('Item 2');
     List.Add('Item 3');
     List.Add('Item 4');
     List.Add('Item 5');
     List.Add('Item 6');
     List.Add('Item 7');
 
     // Check new capacity
     ShowMessage('New capacity: '+ IntToStr(List.Capacity));
   finally
     List.Free;
   }
 }
 
 {
   TclStringListCapacityOrnegi;
 }

Output:

Example : Usage of Count


 var
   MyList: TclStringList;
   ItemCount: Integer;
 {
   MyList = Clomosy.StringListNew;
 
   // Add items
   MyList.Add('Apple');
   MyList.Add('Banana');
   MyList.Add('Orange');
 
   // Get item count
   ItemCount = MyList.Count;
   
   // Show result
   ShowMessage('There are ' + IntToStr(ItemCount) + ' items in the list.');
   
   MyList.Free;
 }

Output:

Example : Usage of Sorted


 var
   MyList: TclStringList;
   i: Integer;
 {
   MyList = Clomosy.StringListNew;
   
   // Add unordered items
   MyList.Add('Orange');
   MyList.Add('Banana');
   MyList.Add('Apple');
   
   // Show unordered list items
   ShowMessage('Unsorted List:');
   for (i = 0 to MyList.Count - 1)
     ShowMessage(Clomosy.StringListItemString(MyList,i));
   
   // Sort items
   MyList.Sorted = True;
   
   // Show sorted list items
   ShowMessage('Sorted List:');
   for (i = 0 to MyList.Count - 1)
     ShowMessage(Clomosy.StringListItemString(MyList,i));
   MyList.Free;
 }

Output:

Example : Usage of String Splitting


 var
  list:TclStringList;
  str:String;
 {
   str = 'hello,world,test';
   list = Clomosy.StringListNew;
   
   List.StrictDelimiter = True;
   List.Delimiter = ',';
   List.DelimitedText = str; 
   Str= Clomosy.StringListItemString(List,0);//Returns element 0
   ShowMessage(str);
   ShowMessage(List.Count);
 }

Example : Usage of Add


 var
   MyList: TclStringList;
 {
   MyList = Clomosy.StringListNew;
   
   MyList.Add('Blue');
   MyList.Add('Red');
   MyList.Add('Yellow');
   MyList.Add('Purple');
 
   ShowMessage('Items: ' + MyList.Text);
   MyList.Free;
 }

Output:

Example : Usage of Insert


 var
   MyList: TclStringList;
 {
   MyList = Clomosy.StringListNew;
   
   MyList.Add('Apple');
   MyList.Add('Banana');
   MyList.Add('Orange');
   
   // Add "Grape" to Index 1
   MyList.Insert(1, 'Grape');
   
   ShowMessage('Items: ' + MyList.Text);
   
   MyList.Free;
 }

As a result, the contents of the list are displayed using the ShowMessage function.

Output:

Example : Usage of Delete


 var
   MyList: TclStringList;
 {
   MyList = Clomosy.StringListNew;
   
   MyList.Add('Apple');
   MyList.Add('Banana');
   MyList.Add('Orange');
   
   // Delete "Grape" to Index 1
   MyList.Delete(1);
   
   ShowMessage('Items: ' + MyList.Text);
   
   MyList.Free;
 }

Output:

Example : Usage of Clear


 var
   MyList: TclStringList;
 {
   MyList = Clomosy.StringListNew;
   
   MyList.Add('Apple');
   MyList.Add('Banana');
   MyList.Add('Orange');
   ShowMessage('Items: ' + MyList.Text);
   
   // Clear "Grape" to Index 1
   MyList.Clear;
   
   ShowMessage('Items: ' + MyList.Text);
   
   MyList.Free;
 }

As a result, the updated content of the list is displayed using the ShowMessage function. You will get a message like "Items:" (empty) as output because all items have been deleted.


Example : Usage of IndexOf


 var
   MyList: TclStringList;
   Index: Integer;
 {
   MyList = Clomosy.StringListNew;
   
   MyList.Add('Apple');
   MyList.Add('Banana');
   MyList.Add('Orange');
   
   Index = MyList.IndexOf('Banana');
   
   ShowMessage('Indeks: ' + IntToStr(Index));
   
   MyList.Free;
 }

Output:

Example : Usage of Exchange


 var
   MyList: TclStringList;
   Index: Integer;
 {
   MyList = Clomosy.StringListNew;
  
   MyList.Add('Apple');
   MyList.Add('Banana');
   MyList.Add('Orange');
   
   MyList.Exchange(0,2);
   
   ShowMessage('Items: ' + MyList.Text);
   
   MyList.Free;
 }

Output:

Example : Usage of Sort


 var
   MyList: TclStringList;
   Index: Integer;
 {
   MyList = Clomosy.StringListNew;
   
   MyList.Add('Orange');
   MyList.Add('Banana');
   MyList.Add('Apple');
   
   ShowMessage('Items: ' + MyList.Text);
   
   MyList.Sort;
   
   ShowMessage('Items: ' + MyList.Text);
   MyList.Free;
 }

Output:

Example : Install from File / LoadFromFile


 var 
   strList:TclStringList;
   clQueryLoadFile : TclJsonQuery;
   path:String;
 {
   strList = Clomosy.StringListNew;
   path = Clomosy.AppFilesPath+'note.txt';
   //ShowMessage(path);
   strList.LoadFromFile(path,0);
   ShowMessage(strList.Text); // The data in the file is displayed by putting it into a String List.
   clQueryLoadFile = Clomosy.ClDataSetFromJSON(strList.Text);
   ShowMessage(clQueryLoadFile.getJSONString);
 }


Example : Writing to a File / SaveToFile


Using the TclStringList class, lines of text are added to a file and then read from the file and displayed on the screen. First, a TclStringList object named StringList1 and a file path variable named FileStr are defined. The showFile procedure loads the lines from the file into the StringList1 object if the specified file exists, and then displays each line on the screen. In the main program block, the StringList1 object is created, and the FileStr variable is set to point to the MyFile.Txt file located in the application files path. Three lines of text are added to the StringList1 object, and these lines are saved to the file. Finally, the showFile procedure is called to display the lines from the file on the screen, and the StringList1 object is freed.

  var
    StringList1: TclStringList;
    FileStr:String;
    i: Integer;
  void showFile;
  {
    If (clFileExists(FileStr,''))
      StringList1.LoadFromFile(FileStr,0);
      
    for (i = 0 to StringList1.Count - 1)
      ShowMessage(Clomosy.StringListItemString(StringList1,i));
  }
  
  {
    StringList1 = Clomosy.StringListNew;
    FileStr = clPathCombine('MyFile.Txt',Clomosy.AppFilesPath); 
    try
      // Adding rows to TclStringList
      StringList1.Add('This is line one.');
      StringList1.Add('This is line two.');
      StringList1.Add('This is line three.');
  
      // Saving TclStringList to a file
      StringList1.SaveToFile(FileStr,0);
      showFile;
    finally
      StringList1.Free;
    }
  }

See Also