#include<iostream>
#include<string>
using namespace std;
void test() {
string str1 = "I ";//string 的无参构造
const char* s = "wxd, hello!";
str1 += s; //string& operator+=(const char* str);将字符串s连接str1末尾
cout << str1 << endl;
string str2;
str2+= str1; //string& operator+=(const string& s)将字符串s连接str2末尾
cout << str2 << endl;
str2 += 'a'; // string& operator+=(char c)将字符c连接str2末尾
cout << str2 << endl;
str2.append(s); //string& append(const char* s);将字符串s连接str2末尾
cout << str2 << endl;
str2.append(s, 3); //string& append(const char* s,int num);将字符串s前num个连接str2末尾
cout << str2 << endl;
str2.append(str1); //string& append(const string& s);将字符串s连接str2末尾
cout << str2 << endl;
str2.append(str1, 4, 6); //string& append(const string& s,int index,int num);将字符串s从下标index开始的后num个赋给str2
cout << str2 << endl;
}
int main() {
test();
return 0;
}
c++string字符串拼接
最新推荐文章于 2024-06-11 00:19:18 发布