[英]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 . 我试图在功能上复制JavaScript的子字符串,通过给定from / to
pos的子字符串from / to
字符串中提取字符串。 How should I continue this function after I gut the location from
and to
? 我应该怎样继续以后我肠道的位置,此功能from
和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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.