C++string容器-字串拼接
阿新 • • 發佈:2021-02-01
string字串拼接
功能描述:
實現在字串末尾拼接字串
函式原型:
程式碼如下:
#include <iostream>
using namespace std;
#include <cstring>
//string字串拼接
void test01() {
string str1 = "我";
str1 += "愛玩遊戲";
cout << "str1 = " << str1 << endl;
str1 += ';' ;
cout << "str1 = " << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" love");
cout << "str3 = " << str3 << endl;
str3.append(" game abcde", 5);
cout << "str3 = " << str3 << endl;
str3.append(str2);
cout << "str3 = " << str3 << endl;
str3.append(str2, 0, 3);//0,3 表示從第0個位置開始,擷取3個的意思 只截取了LOL
cout << "str3 = " << str3 << endl;
str3.append(str2, 4, 3);//只截取了DNF 引數2 從哪個位置開始擷取 引數3 擷取字串個數
cout << "str3 = " << str3 << endl;
}
int main() {
test01();
return 0;
}