Start New Discussion within our Software Development Community

I am replacing 2 words in a textBox with this code. What happens is that the code scans the textBox and finds 2 words: "one" and "onefour". These two words should be replaced with:

onefour[Calculate][Calculate2]
one[Calculate][Calculate2]

But instead the replacement will look like below.
I know why it happens because first "one" is replaced with
one[Calculate][Calculate2] so "four" in the below case will come after.
Then "onefour" doesn´t exist anymore in the textBox.
I am not sure how I can "isolate" this problem so the replacement will look like the example above ?

one[Calculate][Calculate2]four
one[Calculate][Calculate2]

for( int startP = Compiled2->Text->IndexOf("one"); -1 != startP; startP = Compiled2->Text->IndexOf("one", startP + 3))
{
			
	this->Compiled2->Select( startP, 3);
	this->Compiled2->SelectedText = "one[Calculate][Calculate2]";
	this->Compiled2->DeselectAll();
			
}
for(int startP = Compiled2->Text->IndexOf("onefour"); -1 != startP; startP = Compiled2->Text->IndexOf("onefour", startP + 7))
{
			
	this->Compiled2->Select( startP, 7);
	this->Compiled2->SelectedText = "onefour[Calculate][Calculate2]";
	this->Compiled2->DeselectAll();
			
}

I found a solution but I am not sure what I have done. It works though:

if (Compiled2->Text->IndexOf("one",  4) == 0)
{
for( int startP = Compiled2->Text->IndexOf("one"); -1 != startP; startP = Compiled2->Text->IndexOf("one", startP + 3))
{
			
	this->Compiled2->Select( startP, 3);
	this->Compiled2->SelectedText = "one[Calculate][Calculate2]";
	this->Compiled2->DeselectAll();
			
}
}
This question has already been answered. Start a new discussion instead.