目录
1.substr的基本使用
函数形式:std::string substr(size_t pos = 0, size_t n = npos) const;
函数使用:str.substr(pos, n);
【pos:起始位置 n:阶段长度】
注:参数二n可以没有,但参数一pos必须有
string strPath = "H:\\SDevelop\\Dept\\PersonalFiles\\TestDlg.cpp";
string strPath2 = strPath.substr(0, 4); // H:\S
string strPath3 = strPath.substr(7); //elop\\Dept\\PersonalFiles\\TestDlg.cpp
2.利用find...等std::string函数
可参考作者另一篇博客:函数std::string::find,rfind,find_first_of,find_last_of,find_first_not_of,find_last_not_of的基本使用-CSDN博客
3. 获取不同位置的文件名或路径
(1)获取路径的头部和尾部路径:
string strPath13 = "H:\\Project001\\Code\\Test\\Test\\TestDlg.cpp";
string strFile = "";
strFile = strPath13.substr(0, strPath.find("\\")); //H:
strFile = strPath13.substr(strPath.rfind("\\")-1); //TestDlg.cpp
(2)获取路径中各个文件夹:
string strPath13 = "H:\\Project001\\Code\\Test\\Test\\TestDlg.cpp";
string strFile = "";
int iPos1 = 0;
int iPos2 = strPath13.find("\\");
while(iPos2 != std::string::npos)
{
strFile = strPath13.substr(iPos1, iPos2 - iPos1);
iPos1 = iPos2;
iPos2 = strPath13.find("\\", iPos1 + 1);
}
输出:
H:
\Project001
\Code
\Test
\Test
442




被折叠的 条评论
为什么被折叠?



