string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos); 其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子字符串首次出现的位置,否则返回-1; 注: (1)find_last_of的npos为从末尾开始寻找的位置。 (2)下文中用到的strsub(npos,size)函数,其中npos为开始位置,size为截取大小 例1:直接查找字符串中是否具有某个字符串(返回”2″) std::string strPath = E:\\ 在C++编程中,`std::string`是一个非常重要的数据类型,用于表示和操作字符串。本文将详细解析两种常用的C++ `std::string`截取字符串的方法:`find`和`find_last_of`,以及如何结合使用它们来满足各种字符串处理需求。 1. `find`方法: `find`方法用于在字符串中查找指定子字符串`strSub`的第一次出现。它的基本语法是`find(string strSub, npos)`,其中`strSub`是要查找的子字符串,`npos`表示搜索的起始位置。如果找到`strSub`,`find`返回子字符串在原字符串中的起始位置;若未找到,返回`std::string::npos`。例如: ```cpp std::string strPath = "E:\\数据\\2018\\2000坐标系\\a.shp"; int a = 0; if (strPath.find("2018") == std::string::npos) { a = 1; } else { a = 2; } ``` 上述代码检查`strPath`中是否存在子字符串"2018",如果存在,则`a`被赋值为2,否则为1。 2. `find_last_of`方法: `find_last_of`方法则从右向左查找,寻找与`strSub`中的任意字符匹配的字符。其基本语法也是`find_last_of(string strSub, npos)`。这里的`npos`是从字符串末尾开始查找的位置。如果找到匹配的字符,`find_last_of`返回匹配字符的索引;否则返回`std::string::npos`。例如: ```cpp std::string strPath = "E:\\数据\\2018\\2000坐标系\\a.shp"; int nPos1 = strPath.find_last_of("\\"); ``` 这段代码找到`strPath`中最右边的反斜杠位置,便于获取路径的目录部分。 3. 结合使用`find`和`find_last_of`进行截取: 通过结合使用这两个方法,我们可以获取特定子字符串之间的内容。例如: ```cpp std::string strPath = "E:\\数据\\2018\\2000坐标系\\a.shp"; std::string::size_type nPos1 = strPath.find_last_of("\\"); std::string::size_type nPos2 = strPath.find_last_of("\\", nPos1 - 1); if (nPos1 != -1 && nPos2 != -1) { strPath = strPath.substr(nPos2 + 1, nPos1 - nPos2 - 1); } ``` 这里,我们找到了两个反斜杠,并截取了它们之间的子字符串"2000坐标系"。 4. 递归获取路径名中的子目录: 为了获取路径名中的子目录,可以使用递归函数`_GetSubPath`,它通过`find_last_of`查找并返回指定级别的子目录: ```cpp bool _GetSubPath(std::string& strPath, std::string& strSubPath, int nSearch) { if (-1 == nSearch || strPath.empty()) return false; std::string::size_type nPos1 = strPath.find_last_of("\\"); if (nPos1 != -1) { strSubPath = strPath.substr(nPos1 + 1); int nNewSearch = nSearch > 1 ? nSearch - 1 : -1; _GetSubPath(strPath.substr(0, nPos1), strSubPath, nNewSearch); } return true; } ``` 这个函数可以从路径名中提取最后一级子目录,并在需要时继续递归获取更高级别的子目录。 通过熟练掌握`std::string`的`find`和`find_last_of`方法,开发者可以方便地处理各种字符串截取和查找任务。此外,结合递归技术,还能处理更复杂的字符串操作,如分割路径名获取子目录。这些基础知识对于C++程序员来说是非常重要的,因为它们构成了字符串处理能力的基础。不断练习和理解这些概念,将有助于提升C++编程技能。
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/release/download_crawler_static/14874161/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 6
- 资源: 886
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)