1.初始化
string s1 = "";
string s2 = "Hello";
string s3(4,'k');
string s4("12345678",1,3);
cout << "s1:" <<s1 << endl;
cout << "s2:" <<s2 << endl;
cout << "s3:" <<s3 << endl;
cout << "s4:" <<s4 << endl;
輸出:
s1:
s2:Hello
s3:kkkk
s4:234
2.string對象賦值
與初始化不同纳寂,使用assign可以修改已經(jīng)創(chuàng)建的string對象的值却汉;
string s5,s6,s7,s8;
s5.assign(s2);
s6.assign(s5,1,3);
s7.assign(4,'k');
s8.assign("12345678",1,3);
cout << "s5:" <<s5 << endl;
cout << "s6:" <<s6 << endl;
cout << "s7:" <<s7 << endl;
cout << "s8:" <<s8 << endl;
輸出:
s5:Hello
s6:ell
s7:kkkk
s8:234
3.string長度
cout << "s5:" <<s5.size() << endl;
cout << "s6:" <<s6.length() << endl;
輸出:
s5:5
s6:3
4.string對象中字符串的連接
s2 += s3; //字符串+
cout << "s2:" <<s2 << endl;
string s11("abcd"),s12("1234");
s11.append(s12);
cout << "s11:" <<s11 << endl;
s11.append(s12,1,3);
cout << "s11:" <<s11 << endl;
s11.append(4,'k');
cout << "s11:" <<s11 << endl;
s11.append("12345678",1,3);
cout << "s11:" <<s11 << endl;
輸出:
s2:Hellokkkk
s11:abcd1234
s11:abcd1234234
s11:abcd1234234kkkk
s11:abcd1234234kkkk234
5.交換string對象的內(nèi)容
s1.assign("west");
s2.assign("east");
s1.swap(s2);
cout << "s1:" <<s1 << endl;
cout << "s2:" <<s2 << endl;
輸出:
s1:east
s2:west
6.查找子串和字符
string 類有一些查找子串和字符的成員函數(shù),它們的返回值都是子串或字符在 string 對象字符串中的位置(即下標(biāo))蜡豹。如果查不到,則返回 string::npos嘿般。string: :npos 是在 string 類中定義的一個靜態(tài)常量先壕。這些函數(shù)如下:
find:從前往后查找子串或字符出現(xiàn)的位置嗡害。
rfind:從后往前查找子串或字符出現(xiàn)的位置。
find_first_of:從前往后查找何處出現(xiàn)另一個字符串中包含的字符兰怠。例如:
s1.find_first_of("abc"); //查找s1中第一次出現(xiàn)"abc"中任一字符的位置
find_last_of:從后往前查找何處出現(xiàn)另一個字符串中包含的字符梦鉴。
find_first_not_of:從前往后查找何處出現(xiàn)另一個字符串中沒有包含的字符。
find_last_not_of:從后往前查找何處出現(xiàn)另一個字符串中沒有包含的字符揭保。
1. #include <iostream>
2. #include <string>
3. u[sin](http://c.biancheng.net/ref/sin.html)g namespace std;
4. int main()
5. {
6. string s1("Source Code");
7. int n;
8. if ((n = s1.find('u')) != string::npos) //查找 u 出現(xiàn)的位置
9. cout << "1) " << n << "," << s1.substr(n) << endl;
10. //輸出 l)2,urce Code
11. if ((n = s1.find("Source", 3)) == string::npos)
12. //從下標(biāo)3開始查找"Source"肥橙,找不到
13. cout << "2) " << "Not Found" << endl; //輸出 2) Not Found
14. if ((n = s1.find("Co")) != string::npos)
15. //查找子串"Co"。能找到秸侣,返回"Co"的位置
16. cout << "3) " << n << ", " << s1.substr(n) << endl;
17. //輸出 3) 7, Code
18. if ((n = s1.find_first_of("ceo")) != string::npos)
19. //查找第一次出現(xiàn)或 'c'存筏、'e'或'o'的位置
20. cout << "4) " << n << ", " << s1.substr(n) << endl;
21. //輸出 4) l, ource Code
22. if ((n = s1.find_last_of('e')) != string::npos)
23. //查找最后一個 'e' 的位置
24. cout << "5) " << n << ", " << s1.substr(n) << endl; //輸出 5) 10, e
25. if ((n = s1.find_first_not_of("eou", 1)) != string::npos)
26. //從下標(biāo)1開始查找第一次出現(xiàn)非 'e'、'o' 或 'u' 字符的位置
27. cout << "6) " << n << ", " << s1.substr(n) << endl;
28. //輸出 6) 3, rce Code
29. return 0;
30. }
7.替換子串
replace 成員函數(shù)可以對 string 對象中的子串進(jìn)行替換味榛,返回值為對象自身的引用椭坚。例如:
string s1("Real Steel");
s1.replace(1, 3, "123456", 2, 4); //用 "123456" 的子串(2,4) 替換 s1 的子串(1,3)
cout << s1 << endl; //輸出 R3456 Steel
string s2("Harry Potter");
s2.replace(2, 3, 5, '0'); //用 5 個 '0' 替換子串(2,3)
cout << s2 << endl; //輸出 HaOOOOO Potter
int n = s2.find("OOOOO"); //查找子串 "00000" 的位置,n=2
s2.replace(n, 5, "XXX"); //將子串(n,5)替換為"XXX"
cout << s2 < < endl; //輸出 HaXXX Potter
8. 刪除子串
erase 成員函數(shù)可以刪除 string 對象中的子串搏色,返回值為對象自身的引用善茎。例如:
string s1("Real Steel");
s1.erase(1, 3); //刪除子串(1, 3),此后 s1 = "R Steel"
s1.erase(5); //刪除下標(biāo)5及其后面的所有字符频轿,此后 s1 = "R Ste"
9.插入字符串
nsert 成員函數(shù)可以在 string 對象中插入另一個字符串垂涯,返回值為對象自身的引用。例如:
string s1("Limitless"), s2("00");
s1.insert(2, "123"); //在下標(biāo) 2 處插入字符串"123"航邢,s1 = "Li123mitless"
s1.insert(3, s2); //在下標(biāo) 2 處插入 s2 , s1 = "Li10023mitless"
s1.insert(3, 5, 'X'); //在下標(biāo) 3 處插入 5 個 'X'耕赘,s1 = "Li1XXXXX0023mitless"