C++中substr()详解

C++中substr()详解

#include
#include
using namespace std;
int main()
{
  string s("12345asdf");
  string a = s.substr(0,5);     //获得字符串s中从第0位开始的长度为5的字符串
  cout << a << endl;
}

ps:注意substr的第二参数是偏移量不是位置坐标

12345
std::basic_string_view::substr
constexpr basic_string_view
    substr(size_type pos = 0, size_type count = npos ) const;

返回子串 [pos, pos + rcount) 的视图,其中 rcount 是 count 与 size() - pos 中较小者。

参数
pos	-	首字符的位置
count	-	请求的长度
返回值
子串 [pos, pos + rcount) 的视图。

异常
若 pos > size() 则抛出 std::out_of_range 。

复杂度
常数。

你可能感兴趣的:(C++,c++)