目录

substr

描述 (Description)

它返回一个新构造的字符串对象,其值初始化为该对象的子字符串的副本。

声明 (Declaration)

以下是std :: string :: substr的声明。

string substr (size_t pos = 0, size_t len = npos) const;

C++11

string substr (size_t pos = 0, size_t len = npos) const;

C++14

string substr (size_t pos = 0, size_t len = npos) const;

参数 (Parameters)

  • str - 它是一个字符串对象。

  • len - 用于复制字符。

  • pos - 要复制的第一个字符的位置。

返回值 (Return Value)

它返回一个带有该对象子串的字符串对象。

异常 (Exceptions)

如果抛出异常,则字符串中没有变化。

例子 (Example)

在下面的示例中为std :: string :: substr。

#include <iostream>
#include <string>
int main () {
   std::string str="Tutorialspoit is a one the best site in the world, hope so it will move same .";
   std::string str2 = str.substr (3,5);
   std::size_t pos = str.find("live");
   std::string str3 = str.substr (pos);
   std::cout << str2 << ' ' << str3 << '\n';
   return 0;
}

样本输出应该是这样的 -

Hello, 1!
↑回到顶部↑
WIKI教程 @2018