简体   繁体   中英

C++ stl way to extract substring from string from -> to (like Javascript substring)

I'm trying to replicate the same functionally as JavaScript's sub string to extract string from string by given the sub string from / to pos's . How should I continue this function after I gut the location from and to ?

std::string& UT::extractid(std::string& url)
{
    std::vector< std::string > tokens;
    std::string id = "";
    tokens = split(url,'v=');
    video_id = tokens.at(1);
    if(video_id.length()>2)
    {
        string::size_type ampersandPosition = video_id.find('&', 0 );
        if( ampersandPosition != string::npos ) {
            cout << "Found  at " << loc << endl;
        } else {
            cout << "Didn't find  " << endl;
        }
    }
    return video_id;
}

Instead of

return video_id;

do this:

size_t start = video_id.find_first_of("/");
if (start == string::npos) start = 0;
assert(ampersandPosition > start);
return video_id.substr(start, ampersandPosition - start);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM