結合思維導圖和示例代碼學習。
string構造函數
#include <iostream>#include <string>using namespace std;void test(){ string st1; cout << "st1:" << st1 << endl; const char* str = "hello ttbj"; cout << "str:" << str << endl; string st2(str); cout << "st2:" << st2 << endl; string st3(10, 'a'); cout << "st3:" << st3 << endl; string st4(st3); cout << "st4:" << st4 << endl;}int main(){ test(); return 0;}總結:string的多種構造方式沒有可比性,靈活使用即可
string賦值操作
#include<iostream>using namespace std;void test(){ string str1; str1 = "hello ttbj"; cout << "str1 = " << str1 << endl; string str2; str2 = str1; cout << "str2 = " << str2 << endl; string str3; str3 = 'a'; cout << "str3 = " << str3 << endl; string str4; str4.assign("hello xrbq"); cout << "str4 = " << str4 << endl; string str5; str5.assign("hello xrbq", 5); cout << "str5 = " << str5 << endl; string str6; str6.assign(str5); cout << "str6 = " << str6 << endl; string str7; str7.assign(5, 'a'); cout << "str7 = " << str7 << endl;}int main(){ test(); return 0;}執行結果:
str1 = hello ttbj
str2 = hello ttbj
str3 = a
str4 = hello xrbq
str5 = hello
str6 = hello
str7 = aaaaa
總結:string賦值方式很多,operator=這種方式是比較實用的
string字符串拼接
#include<iostream>using namespace std;void test(){ string str1 = "我"; str1 += "愛吃東西"; cout << "str1 = " << str1 << endl; str1 += ':'; cout << "str1 = " << str1 << endl; string str2 = "紅燒肉"; str1 += str2; cout << "str1 = " << str1 << endl; string str3 = "I"; str3.append(" like "); str3.append("eat abc", 4); str3.append(str2); str3.append("str2", 3, 1); cout << "str3 = " << str3 << endl;}int main(){ test(); return 0;}執行結果:
str1 = 我愛吃東西
str1 = 我愛吃東西:
str1 = 我愛吃東西:紅燒肉
str3 = I like eat 紅燒肉2
總結:字符串拼接重載版本很多,記住幾種即可。
string查找和替換
void test(){ string str1 = "abcdefgde"; int pos = str1.find("de"); if(pos == -1) { cout << "未找到" << endl; } else { cout << "pos = " << pos << endl; } pos = str1.rfind("de"); cout << "pos = " << pos << endl;
str1.replace(1, 3, "1111"); cout << "str1 = " << str1 << endl;}int main(){ test(); return 0;}執行結果:
pos = 3
pos = 7
str1 = a1111efgde
總結:
Find查找是從左往右,rfind從右往左
Find找到字符串後返回查找的第一個字符位置,找不到返回-1
replace在替換時,要指定從哪個位置起,多少個字符,替換成什麼樣的字符串
string字符串比較
void test(){ string s1 = "xrbq"; string s2 = "ttbj"; int ret = s1.compare(s2); if(0 == ret) { cout << "s1 等於 s2" << endl; } else if(ret > 0) { cout << "s1 大於 s2" << endl; } else { cout << "s1 小於 s2" << endl; } }int main(){ test(); return 0;}執行結果:
s1 大於 s2
總結:
字符串對比主要是看兩個字符串是否相等,判斷誰大誰小意義並不是很大
string字符存取
void test(){ string str = "hello world"; for(int i = 0; i < str.size(); i++) { cout << str[i] << " "; } cout << endl;
for(int i = 0; i < str.size(); i++) { cout << str.at(i) << " "; } cout << endl; str[0] = 'y'; str.at(1) = 'y'; cout << str << endl;}int main(){ test(); return 0;}執行結果:
h e l l o w o r l d
h e l l o w o r l d
yyllo world
總結:string字符串中單個字符存取有兩種方式,利用[]或at
string 插入和刪除
void test(){ string str = "hello"; str.insert(1, "111"); cout << str << endl; str.erase(1, 3); cout << str << endl;}int main(){ test(); return 0;}執行結果:
h111ello
hello
總結:插入和刪除的起始下標都是從0開始
string子串
void test(){ string email = "hello@qq.com"; int pos = email.find("@"); string username = email.substr(0, pos); cout << "username: " << username << endl;}int main(){ test(); return 0;}執行結果:
username: hello
總結:靈活運用求子串功能,可以在實際開發中獲取有效的信息
- END -