Главная страница | назад





Article #19319: Replacing chars in a stringlist

 Question and Answer Database
FAQ4339C.txt :Replacing chars in a stringlist
Category :VCL
Platform :All Win32
Product :C++Builder1.0, C++Builder3.x, C++Builder4.x,
Question:
I have the need to be able to search and replace certain characters in a list of strings,
I seem to be having trouble trying to figure out how his is done, do you have any suggestions?
Answer:
Ine problem you may be running into is trying to replace the characters 'in place',
the problem with this is that the VCL often returns temporaries when you access
properties, to get around this, you will need to do an assignment in the right place.
The following code shows how:
//---------------------------------------------------
void __fastcall TForm1::GoClick(TObject *Sender)
{
for (int i = 0; i < ListBox1->Items->Count; i++)
{
String temp = ListBox1->Items->Strings[i];
bool replace = false;
for (int j = 1; j <= temp.Length(); j++)
{
if (temp[j] == Find->Text[1])
{
temp[j] = Replace->Text[1];
replace = true;
}
}
if (replace == true)
{
ListBox1->Items->Strings[i] = temp;
}
}
}
//---------------------------------------------------
This code assumes that you have a listbox called ListBox1,
2 editboxes called Find and Replace and a button called Go.
1/21/1999 3:05:35 PM

Last Modified: 01-SEP-99