//假设我有一个字符串路径:
string path = "D:\Program Files\opencv\A1.jpg";
//或者是这样的路径:
string path = "D:/Program Files/opencv/B11.jpg";
简而言之,就是获取路径中的文件名字“A1.jpg”和“B11.jpg”,如何用C++编程实现
C/C++字符串截取的问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
21条回答 默认 最新
- haidaolongdehao 2018-05-31 11:25关注
#include
#includeusing namespace std;
int main()
{
string strPath(""D:\Program Files\opencv\A1.jpg""); // string中的\是转义字符,\就是代表\
string strFileName;
int nPos = strPath.find_last_of('\');
if(nPos > 0)
{
strFileName = strPath.substr(nPos + 1, strPath.length() - nPos - 1);
cout << strFileName << endl;
}
return 0;
}另外一种情况类似:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报