首页 > C++入门教程 > C++ String类型
C++提取子字符串
substr() 函数可以提取 string 字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串的起始下标,第二个参数是需要提取的子字符串的长度。
举个例子:
声明:《C++系列教程》为本站“54笨鸟”官方原创,由国家机构和地方版权局所签发的权威证书所保护。
举个例子:
#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
声明:《C++系列教程》为本站“54笨鸟”官方原创,由国家机构和地方版权局所签发的权威证书所保护。
ICP备案:
公安联网备案: