I have a richTextBox where I am trying to replace all strings that look like this:
"stringvec[11][12]".
I have this for loop so far that would work if the string always had the same lentgh but the thing is that I will not know if the string also can look like this: "stringvec[1][2]" wich gives another length.
So what I beleive is to find a way to know when the string has ended or find the last "]" of the string.

My problem is then to know how to define the second argument for:
Select( startP, );

I have tried something in the code as seen but this does not work.

for(int startP = richTextBox1->Text->IndexOf("stringvec["); 
-1 != startP; 
startP = richTextBox1->Text->IndexOf("stringvec[", startP + 10))
{
	int endP = richTextBox1->Text->IndexOf("]");
		
	this->richTextBox1->Select( startP, (endP - startP));
	this->richTextBox1->SelectedText = "word2";
	this->richTextBox1->DeselectAll();
}

Recommended Answers

All 3 Replies

>>find a way to know when the string has ended

look for a null char, that's the end of the string.

>>find the last "]" of the string.

Probably the easiest way is to start at the end of the string and search backwards through the string, though, if the string is always of the form example[][] then the last ] is at the index equal to the length of the string minus 1.

I have contined some steps ahead with my problem, though I cant understand what to do in order to make this work.
I have made this code that almost works, it must be a detail that I am missin somewhere.
I am trying to extract two numbers from:

thisstring[10][20]

As seen I am searhing for this with stringToFind5 in Line10 .

In the code I am declaring Number5 to 10 wich works fine.
Now my problem is to extract number 20. I have found the startPos wich is the
(endPos5 + 2).

The problem I have is to find the last "]" in the string to extract: 20 correctly.
How the extraction look like now for Number6 is: 20] and four

What could I be doing wrong, I know the problem lies on line 16 ?

std::string Line10 = "one two three thisstring[10][20] and four";
string stringToFind5 = "thisstring[";


std::string::size_type startPos5 = Line10.find(stringToFind5);
      
if ( startPos5 != std::string::npos )
{
startPos5 += stringToFind5.length(); 
std::string::size_type endPos5 = Line10.find("]", startPos5); 

if ( endPos5 != std::string::npos )
{
std::string Number5 = Line10.substr(startPos5, endPos5 - startPos5); 
std::string::size_type endPos6 = Line10.find("]",endPos5);
std::string Number6 = Line10.substr( (endPos5 + 2), endPos6 - (endPos5 + 3)  );
            
if ( Line10.find(stringToFind5) != std::string::npos )
{
             
  String^ showmessage = gcnew String(Number6.c_str());
  MessageBox::Show(showmessage);
			  
    }
  }
}

>>find a way to know when the string has ended

look for a null char, that's the end of the string.

>>find the last "]" of the string.

Probably the easiest way is to start at the end of the string and search backwards through the string, though, if the string is always of the form example[][] then the last ] is at the index equal to the length of the string minus 1.

I think I found the problem, I changed the line 15 to this. I beleive this was the problem:

std::string::size_type endPos6 = Line10.find("]",(endPos5 + 2));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.