You are here

Manipulating strings in a StringList

Submitted by Asif Nowaj, Last Modified on 2019-11-22
Know number of strings in the list
StringList.Count provides the number of strings within the list. It is a zero based indexed list. So Count value is one more than the last index of the list.
Accessing a particular string
TStringList is referenced by a zero-based index. You can access any string by its index position in ways either by using Strings property or directly. Example is show below. Strings is the default property for string lists.
StringList1.Strings[0] := 'This is your first string.';
is equivalent to
StringList1[0] := 'This is your first string.';
Locating items in a string list
If you want to locate a string or wants to know whether a string is present within a list then you can do that by using IndexOf property. IndexOf returns the position of the string passed to it and it matches for exact match. If a string is not present or not matched exactly then it returns -1.
Example if FileListBox1.Items.IndexOf('TargetFileName') > -1 to check whether the “TargetFileName” is present within the list of filenames.
Iterating through strings in a list
To iterate through the strings in a list, use a for loop that runs from zero to Count - 1.
Adding a string to a list
To add a string to the end of a string list, call the Add method, passing the new string as the parameter. To insert a string into the list, call the Insert method, passing two parameters: the string and the index of the position where you want it placed.
For example, to make the string "Bella" the fourth string in a list, you would use:
Insert(3, 'Bella');
To append the strings from one list onto another, call AddStrings:
tempStringList.AddStrings(tempStringList2); { append the strings from tempStringList 2 to tempStringList }
Moving a string within a list
To move a string in a string list, call the Move method, passing two parameters: the current index of the string and the index you want assigned to it. For example, to move the third string in a list to the fourth position, you would use:
tempStringList.Move(2, 3)
Deleting a string from a list
To delete a string from a string list, call the list's Delete method, passing the index of the string you want to delete. If you don't know the index of the string you want to delete, use the IndexOf method to locate it. To delete all the strings in a string list, use the Clear method.
The following example uses IndexOf and Delete to find and delete a string:
with tempStringList do
BIndex := IndexOf('bureaucracy'); if BIndex > -1 then Delete(BIndex);
Copying a complete string list
You can use the Assign method to copy strings from a source list to a destination list, overwriting the contents of the destination list. To append strings without overwriting the destination list, use AddStrings. For example,
Memo1.Lines.Assign(ComboBox1.Items); { overwrites original strings }
copies the lines from a combo box into a memo (overwriting the memo), while
Memo1.Lines.AddStrings(ComboBox1.Items); { appends strings to end }
appends the lines from the combo box to the memo.
When making local copies of a string list, use the Assign method. If you assign one string-list variable to another--
StringList1 := StringList2;
--the original string-list object will be lost, often with unpredictable results.

procedure TForm1.Button3Click(Sender: TObject);
  tempStringList  : TStringList;
  tempStringList := TStringList.Create;
    ShowMessage('Number of strings: '+IntToStr(tempStringList.Count));
    ShowMessage('string at zero-th position: '+ tempStringList[0]);

    if tempStringList.IndexOf('Bella') > -1 then
      ShowMessage('String Bella presents within the list at the position:'+IntToStr(tempStringList.IndexOf('Bella')))
      ShowMessage('String Bella does not present within the list');
    for i:=0 to tempStringList.Count - 1 do

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.