#include <string>
#include <iostream>
std::string extract(std::string& str, char beg, char end)
{
std::size_t begPos ;
if ( (begPos = str.find(beg)) != std::string::npos )
{
std::size_t endPos ;
if ( (endPos = str.find(end, begPos)) != std::string::npos )
{
std::string result = str.substr(begPos+1, endPos-begPos-1) ;
str.erase(begPos, endPos-begPos+1) ;
return result ;
}
}
return std::string() ;
}
int main()
{
std::string original("This is the string of text $Iwantthis+$Iwantthistoo text, end of the string.") ;
std::cout << extract(original, '$', '+') << '\n' ;
std::cout << extract(original, '$', ' ') << '\n' ;
std::cout << "Original modified to \"" << original << "\"\n" ;
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RkOjpzdHJpbmcgZXh0cmFjdChzdGQ6OnN0cmluZyYgc3RyLCBjaGFyIGJlZywgY2hhciBlbmQpCnsKICAgIHN0ZDo6c2l6ZV90IGJlZ1BvcyA7CiAgICBpZiAoIChiZWdQb3MgPSBzdHIuZmluZChiZWcpKSAhPSBzdGQ6OnN0cmluZzo6bnBvcyApCiAgICB7CiAgICAgICAgc3RkOjpzaXplX3QgZW5kUG9zIDsKICAgICAgICBpZiAoIChlbmRQb3MgPSBzdHIuZmluZChlbmQsIGJlZ1BvcykpICE9IHN0ZDo6c3RyaW5nOjpucG9zICkKICAgICAgICB7CiAgICAgICAgICAgIHN0ZDo6c3RyaW5nIHJlc3VsdCA9IHN0ci5zdWJzdHIoYmVnUG9zKzEsIGVuZFBvcy1iZWdQb3MtMSkgOwogICAgICAgICAgICBzdHIuZXJhc2UoYmVnUG9zLCBlbmRQb3MtYmVnUG9zKzEpIDsKICAgICAgICAgICAgcmV0dXJuIHJlc3VsdCA7CiAgICAgICAgfQogICAgfQoKICAgIHJldHVybiBzdGQ6OnN0cmluZygpIDsKfQoKaW50IG1haW4oKQp7CiAgICBzdGQ6OnN0cmluZyBvcmlnaW5hbCgiVGhpcyBpcyB0aGUgc3RyaW5nIG9mIHRleHQgJEl3YW50dGhpcyskSXdhb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