开发者

Search a std::string

开发者 https://www.devze.com 2023-01-04 09:52 出处:网络
I\'m looking to search a string for example: std::string str = \"_Da开发者_开发问答ta_End__Begin_Data_End\";

I'm looking to search a string for example:

std::string str = "_Da开发者_开发问答ta_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?


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

str.find("Begin", length_of_data_section1);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号