我在c ++中有一个文件名列表,如字符串。我将它们存储在向量中。
vector<string> files;
使用.find_last_of
,我得到了扩展名。
for (int j = 0; j < files.size(); j++)
{
if (files[j].substr(files[j].find_last_of(".") + 1) == "tif") {
images.push_back(files[j]);
}
这一切都很好。现在,我需要检查文件名的最后一部分,在扩展名之前,例如,在文件中:
FileName.1001.tif
我正在寻找 '1001'
我试过了:
if (files[i].substr(files[i].find_last_of(".") - 1) == "1001")
{
std::cout << "yaaay..." << std::endl;
}
但它永远不会受到打击。我可以在这里不使用“-1”吗?还是我错过了什么?
坚持使用当前的方法,您只需要删除扩展名,然后应用相同的“查找最后一个”即可。逻辑:
std::string getLastPart(const std::string &filename)
{
// Strip of the extension
auto name = filename.substr(0, filename.find_last_of('.'));
// Now get everything to the right of the last .
auto part = name.substr(name.find_last_of('.') + 1);
return part;
}
注意:如果.
不存在,您需要添加一些错误处理...
现在您可以说:
if(getLastPart(files[i]) == "1001")
{
std::cout << "yaaay..." << std::endl;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句