一
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1(8, 'x');
cout << s1 << endl;
string s2;
cin >> s2;
s1.assign(s1, 0, 1);
cout << s1 << endl;
system("pause");
return 0;
}
assign(0,1)函数可以实现从下标为0开始往后1个字符赋值
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("hello");
string s2("good");
s1.append(s2, 1, s1.size());
cout << s1;
system("pause");
return 0;
}
append()函数实现部分字符串的拼接,如若长度不够,访问到s2的末尾
#include <iostream>
#include<string>
#include<cstring>
using namespace std;
int main(int argc, char *argv[])
{
string s1,s2;
s1="1234";
s2="56";
swap(s1,s2);
cout<<s1<<" "<<s2<<endl;
swap(s1,s2);
s1=s2;
cout<<s1;
return 0;
}