tags:

views:

81

answers:

2

I'm looking to search a string for example:

std::string str = "_Data_End__Begin_Data_End";
                  |_________| |_____________|
                      data        data
                    section1     section2

If i'm searching for "Begin" in str i want to make sure that it does look in "data section2" if it wasn't found in "data section1"

if i knew the length of "data section1" would it be possible?

+2  A: 

std::string::find() has an optional argument to indicate where to begin searching:

str.find("Begin", length_of_data_section1);
Thomas
but what about searching for it in data_section1?
Carl17
@Carl17: `rfind()`: http://www.cplusplus.com/reference/string/string/rfind/
Brendan Long
+2  A: 

The following code searches the string for "Begin".

std::string::size_type loc = str.find("Begin");
if(loc != std::string::npos)
{
    std::cout << "found \"Begin\" at position " << loc << std::endl;
}

And because data section2 is at the end of data section1 (and find() begins searching at position0) you don't need any more code.

bb-generation