Programming C++Builder Manipulating Strings in a List in C++Builder
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

Operations commonly performed on string lists include:

Counting the strings in a list

The Strings array property contains the strings in the list, referenced by a zero-based index.

StringList1->Strings[0] = "This is the first string.";

Accessing a particular string

The Strings array property contains the strings in the list, referenced by a zero-based index.

StringList1->Strings[0] = "This is the first string.";

Finding the position of a string in the list

To locate a string in a string list, use the IndexOf method. IndexOf returns the index of the first string in the list that matches the parameter passed to it, and returns ? if the parameter string is not found. IndexOf finds exact matches only; if you want to match partial strings, you must iterate through the string list yourself.
For example, you could use IndexOf to determine whether a given file name is found among the Items of a list box:

if (FileListBox1->Items->IndexOf("WIN.INI") > -1) ...

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.
This example converts each string in a list box to uppercase characters.

void __fastcallTForm1::Button1Click(TObject *Sender)

{
  for (int i = 0; i Items->Count; i++)
    ListBox1->Items->Strings[i] = UpperCase(ListBox1->Items->Strings[i]);
}

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 揟hree?the third string in a list, you would use:

StringList1->Insert(2, "Three");

To append the strings from one list onto another, call AddStrings:

StringList1->AddStrings(StringList2);  // append the strings from StringList2 to StringList1

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 fifth position, you would use:

StringListObject->Move(2, 4);

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抰 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.
This example uses IndexOf and Delete to find and delete a string:

int BIndex = ListBox1->Items->IndexOf("bureaucracy");

if (BIndex > -1)
  ListBox1->Items->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->Item)s;       //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.


Add comment


Security code
Refresh

Programming - C++Builder