繁体   English   中英

C ++ stl方法从字符串中提取子字符串 - >到(像Javascript子字符串)

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

我试图在功能上复制JavaScript的子字符串,通过给定from / to pos的子字符串from / to字符串中提取字符串。 我应该怎样继续以后我肠道的位置,此功能fromto

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;
}

代替

return video_id;

做这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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