![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Search a TStringList for a specific stringAnswer:Use the IndexOf member function of TStringList. IndexOf returns the arrary index of the string's location in the TStringList. The array indices are zero based: the first string is index 0. IndexOf returns -1 if the string was not found in the TStringList. Here are some code examples. // This code searches TStringList A for a string. // Assume TStringList *A configured elsewhere int nIndex = A->IndexOf("hello"); // This code searches TStringList A for a string, and then // modifies that item if the string is found. // Assume TStringList *A configured elsewhere int nIndex = A->IndexOf("hello"); if (nIndex != -1) A->Strings[nIndex] = "GoodBye"; // This code searches a ListBox for the string "Dr. Dobbs". If // the string is not found, it is added to the ListBox AnsiString str = "Dr. Dobbs"; if(ListBox1->Items->IndexOf(str) == -1) ListBox1->Items->Add(str); else Application->MessageBox("Dr Dobbs found in ListBox1","found",MB_OK); // This code modifies a string in the StringList of a ListBox. // Assume Edit1 provides the string to search for, and Edit2 contains // what that string should be changed to. int nIndex = ListBox1->Items->IndexOf(Edit1->Text); if(nIndex != -1) ListBox1->Items[nIndex] = Edit2->Text; | ||||||
All rights reserved. |