C++ String 库 - substr
描述
它返回一个新构造的字符串对象,其值初始化为该对象的子字符串的副本。
声明
以下是 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;
参数
str − 它是一个字符串对象。
len − 它用于复制字符。
pos − 要复制的第一个字符的位置。
返回值
它返回一个带有该对象子字符串的字符串对象。
异常
如果抛出异常,则字符串没有变化。
示例
在下面的 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!