首页 > C++入门教程 > C++ String类型
C++提取子字符串
substr() 函数可以提取 string 字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串的起始下标,第二个参数是需要提取的子字符串的长度。
举个例子:
举个例子:
#include <iostream> #include <string> using namespace std; int main() { string s1 = "first second third"; string s2; s2 = s1.substr(6, 6); cout<< s1 <<endl; cout<< s2 <<endl; return 0; }程序运行结果:
first second third
second