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:\\数据\\2018\\2000坐标系\\a.shp"
int a = 0;
if (strPath.find("2018") == std::string::npos)
{a = 1;
}
else
{a = 2;
}
return a;

例2:查找某个字符串的字符串(返回“E:”)

std::string strPath = "E:\\数据\\2018\\2000坐标系\\a.shp"
int nPos = strPath.find("\\");
if(nPos != -1)
{strPath = strPath.substr(0, nPos);
}
return strPath;

例3:查找某个字符串中某两个子字符串之间的字符串(返回“2000坐标系”)

std::string strPath = "E:\\数据\\2018\\2000坐标系\\a.shp"
std::string::size_type nPos1 = std::string::npos;
std::string::size_type nPos2 = std::string::npos;
nPos1 = strPath.find_last_of("\\");
nPos2 = strPath.find_last_of("\\", nPos1 - 1);
if(nPos1 !=-1 && npos2 != -1)
{strPath = strPath.substr(nPos2 + 1, nPos1 - nPos2 - 1);
}
return strPath;

提高:递归获取路径名中的子目录

//获取路径名中的子目录:strPath为路径名,strSubPath为输出的子目录,nSearch为从尾向前检索的级别(默认为1级)bool _GetSubPath(std::string& strPath,std::string& strSubPath, int nSearch)
{   if (-1 == nSearch || strPath.empty())return false;std::string::size_type nPos1 = std::string::npos;nPos1 = strPath.find_last_of("\\");if (nPos1 != -1){strSubPath = strPath.substr(nPos1 + 1, strPath.length() - nPos1);int nNewSearch = nSearch > 1 ? nSearch - 1 : -1;_GetSubPath(strPath.substr(0, nPos1), strSubPath, nNewSearch);}return true;
}int main()
{std::string strPath = "E:\\数据\\2018\\2000坐标系\\a.shp";std::string strSubPath = "";if(_GetSubPath(strPath, strSubPath, 1){printf(“返回'a.shp'”);}if(_GetSubPath(strPath, strSubPath, 2){printf(“返回'2000坐标系'”);}
}

C++ string常用截取字符串方法相关推荐

  1. c++语言截取字符串,详解C++ string常用截取字符串方法

    string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos) ...

  2. c语言中截取字符串 sub,详解C++ string常用截取字符串方法

    string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos) ...

  3. String 截取字符串方法——subString()

    String 截取字符串方法subString() String可以使用substring方法进行截取字符串有两种,参数可输入一个或者两个: 单参数:public String substring(i ...

  4. JAVA截取字符串方法

    //截取字符串方法 public static void main(String[] args) { String str="QW/E1/2/45"; //寻找最后一个" ...

  5. php str pa,php截取字符串方法有哪些

    php截取字符串方法有:1.使用substr函数截取字符串:2.使用mb_substr函数截取字符串:3.使用自定义的"function mysubstr($str, $start, $le ...

  6. vue 截取字符串方法

    vue 截取字符串方法 shutStr(){const str = 'abcdef';str = str.slice(0); //返回整个字符串 abcdefstr = str.substring(0 ...

  7. web前端js基础之js的几种“截取字符串”方法

    <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8" ...

  8. html截取字符串的第4位,三种常用JS截取字符串方法 JS截取几位后字符串方法

    JS提供了三个常用的截取字符串的方法,分别是:slice(),substring()和substr(),它们都可以接受一个或两个参数: var stmp = "bnxb.com"; ...

  9. js常用数组字符串方法

    一.常用数组方法 创建数组 let arr = new Array(); // 创建一个新的数组 数组的增删改查 let arr = [1,2,3,4] arr.push('5') // 数组末尾添加 ...

最新文章

  1. 所以,你自认为是一个合格的机器学习工程师?
  2. java与fabric区块链--fabric-ca-server 登记---(2)
  3. Netlog 的数据库及 LAMP 架构
  4. 用PHPcms V9四步完成WAP手机站搭建
  5. 仿明日方舟网页html,《明日方舟》干员列表实现
  6. list index out of bounds_python3基础03列表(list)和元组(tuple)
  7. bigint hive java类型_详解Apache Hudi如何配置各种类型分区
  8. mysql 分析服务_MySQL分析服务器状态_MySQL
  9. android 百度室内地图开发,androidsdk | 百度地图API SDK
  10. 使用ifconfig命令来看网卡的IP,但是,输入命令之后,eht0里面只有 inet6 addr 而没有 inet addr...
  11. 20款超级好用的chrome拓展插件让你的工作效率唰唰唰
  12. imx8 usb otg模式切换
  13. 汉庭加盟:连锁酒店影视房的市场分析
  14. Nginx服务器上安装SSL证书
  15. 篮球队员5V5分组战力差最小
  16. 计算机桌面死机的原因是,电脑屏幕死机了怎么办
  17. Python利用xpath和正则re爬取新浪新闻
  18. leach协议c++代码_leach和leach-c协议仿真
  19. Gartner发布2021年隐私技术成熟度曲线,数字伦理登上顶点
  20. 高通Camx offline Log merge命令

热门文章

  1. 隐藏微信小程序返回首页按钮(小房子)
  2. 【源码阅读】【苦练基本功】Golang内置函数分析
  3. uhs3内存卡有哪些_高速内存卡是什么 3款热门高速内存卡推荐
  4. excel怎么设置自动计算_中建整理:160个Excel自动计算表,计算快效率高数据精准,超实用...
  5. 入行大数据,需要学习哪些编程语言?
  6. 学渣的刷题之旅 leetcode刷题 9. 回文数
  7. 格雷希尔G72L系列法兰式密封快速连接器的应用
  8. 非线性方程的数值解法:牛顿下山法 python
  9. typechoSEO站点收录插件推荐
  10. 优秀的 Node.js 包汇总