string类的substr()函数

头文件

#include<string>

函数原型及用法

basic_string substr(size_type _Off = 0,size_type _Count = npos) const;
//函数原型
string s("123456");
string a = s.substr(0,5);     //获得字符串s中从第0位开始的长度为5的字符串
//a为12345

注意:

  • 如果起始位置超过string大小,则抛出out of range异常
  • 如果起始位置+长度超过string大小,只截取到末尾
发布了45 篇原创文章 · 获赞 0 · 访问量 992

猜你喜欢

转载自blog.csdn.net/qq_41985293/article/details/104158716