簡體   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