C++自帶string類的常用方法
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 int main() 6 { 7 string str1 = "hello"; 8 string* str2 = new string("hello"); 9 string str3 = "world"; 10 11 //獲取字符串長度 12 int length = str1.length(); 13 cout << "調用str.length()函數獲取字符串長度:" << length << endl; 14 cout << endl; 15 16 17 //字符串連接 18 string str4 = str1 + str3; 19 cout << "字符串連接結果:" << str4 << endl; 20 cout << endl; 21 22 23 //字符串比較 24 if (str1 < str3) 25 cout << "字符串比較:" << "str1<str2" << endl; 26 cout << endl; 27 28 29 //獲取字符串的第一個字符 30 string::const_iterator it = str1.begin(); 31 cout << *it << endl; 32 cout << endl; 33 34 35 //獲取字符串的最后一個字符 36 it = str1.end();//end是指向最后一個字符后面的元素,而且不能輸出,所以cout << *it << endl;這樣輸出會報錯 37 it--; 38 cout << *it << endl; 39 cout << endl; 40 41 42 //倒置串 43 reverse(str1.begin(), str1.end()); 44 cout << "倒置串:" << str1 << endl; 45 cout << endl; 46 47 //字符串轉字符數組 48 //不推薦的用法,但是需要了解 49 string a = "abc123"; 50 const char *b;//這里必須為const char *,不能用char *,不然下一句會報錯 51 b = a.c_str(); 52 cout << "a:" << a << endl; 53 cout << "b:" << b << endl; 54 a = "asd456"; 55 cout << "a:" << a << endl; 56 cout << "b:" << b << endl; 57 //推薦用法 58 string c = "abc123"; 59 char *d = new char[20]; 60 strcpy(d, c.c_str());//因為這里沒有直接賦值,所以指針類型可以不用const char * 61 cout << "c:" << c << endl; 62 cout << "d:" << d << endl; 63 c = "asd456"; 64 cout << "c:" << c << endl; 65 cout << "d:" << d << endl; 66 cout << endl; 67 68 69 //查找串 70 //find-從指定位置起向后查找,直到串尾 71 string st1("babbabab"); 72 cout << st1.find('a') << endl;//1,默認從位置0(即第1個字符)開始查找 73 cout << st1.find('a', 2) << endl;//4 在st1中,從位置2(b,包括位置2)開始,查找a,返回首次匹配的位置 74 cout << (st1.find('c', 0) == -1) << endl;//1 75 cout << (st1.find('c', 0) == 4294967295) << endl;//1 兩句均輸出1,原因是計算機中-1和4294967295都表示為32個1(二進制) 76 string st2("aabcbcabcbabcc"); 77 str1 = "abc"; 78 cout << st2.find(str1, 2) << endl;//6,從st2的位置2(b)開始匹配,返回第一次成功匹配時匹配的串(abc)的首字符在st2中的位置,失敗返回-1 79 cout << st2.find("abcdefg", 2, 3) << endl;//6 取abcdefg得前3個字符(abc)參與匹配,相當於st2.find("abc", 2) 80 81 //rfind-從指定位置起向前查找,直到串首 82 cout << st1.rfind('a', 7) << endl;//6 83 84 //find_first_of-在源串中從位置pos起往后查找,只要在源串中遇到一個字符,該字符與目標串中任意一個字符相同,就停止查找,返回該字符在源串中的位置;若匹配失敗,返回-1 85 string str6("bcgjhikl"); 86 string str7("kghlj"); 87 cout << str6.find_first_of(str7, 0) << endl;//2,從str1的第0個字符b開始找,g與str2中的g匹配,停止查找,返回g在str1中的位置2 88 89 //find_last_of-與find_first_of函數相似,只不過查找順序是從指定位置向前 90 string str("abcdecg"); 91 cout << str.find_last_of("hjlywkcipn", 6) << endl;//5,從str的位置6(g)開始向前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5 92 93 //find_first_not_of-在源串中從位置pos開始往后查找,只要在源串遇到一個字符,與目標串中的任意字符都不相同,就停止查找,返回該字符在源串中的位置;若遍歷完整個源串,都找不到滿足條件的字符,則返回-1 94 cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3 從源串str的位置0(a)開始查找,目標串中有a,匹配,..,找d,目標串中沒有d(不匹配),停止查找,返回d在str中的位置3 95 96 //find_last_not_of-與find_first_not_of相似,只不過查找順序是從指定位置向前 97 cout << str.find_last_not_of("kiajbvehfgmlc", 6) << endl;//3 98 99 system("pause"); 100 return 0; 101 102 }
運行結果:
如有不對的地方,非常歡迎給予指導!
如果您覺得這篇文章對您有所幫助,您可以點擊右邊的“打賞”功能,也可以點擊下方的“好文要頂”按鈕,因為這兩種肯定,都讓我更加相信自己所做的工作是有意義的,也是支持我繼續寫下去的最大動力!
感謝您給予的支持!