1.insert
1)string &insert(int p0, const char *s)
功能:在原字符串下标为pos的字符前插入字符串str
返回值:插入字符串后的结果(2)string &insert(int p0, const char *s, int n);
功能:在p0位置插入字符串s的前n个字符
返回值:插入字符串后的结果
2.substr函数
一种构造string的方法
形式 : s.substr(pos, len)
返回值: string,包含s中从pos开始的len个字符的拷贝。pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s。xingsi
- substr函数用于赋值时,会先清空另一个容器的元素
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a = "bad";
string s="kill";
cout << s << endl;
s = a.substr(0, 2);
cout << s << endl;
return 0;
}