字符串操作看似簡單,其實非常重要,不注意的話,經常出現代碼運行結果和自己想要的不一致,甚至崩潰。本文總結了一些構建string對象方法、修改string對象的方法、string類型的操作函數、string類型的查找、string對象的比較。
1 構建string對象方法
首先,為了在我們的程序中使用string類型,我們必須包含頭文件 。如下:
#include
聲明一個字符串變量很簡單:
string Str;
這樣我們就聲明了一個字符串變量,但既然是一個類,就有構造函數和析構函數。上面的聲明沒有傳入參數,所以就直接使用了string的默認的構造函數,這個函數所作的就是把Str初始化為一個空字符串。
String類的構造函數和析構函數如下:
![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZKCAX7PFAABKrYyjFgM842.png)
代碼實例:
#include #include using namespace std; //20200527 測試字符串操作 C語言與CPP編程 int main() { string s1; cout <
** 運行結果**:![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZXKAZUcfAAAWVLX0LRg092.jpg)
2 修改string對象的方法
與容器共有的 string 操作:
![](http://file.elecfans.com/web1/M00/C7/86/pIYBAF9oZL-AEk0TAABdoEdA6JM594.png)
代碼實例:
#include #include using namespace std; //2020.05.27 測試字符串操作 公眾號:C語言與CPP編程 int main() { string s("hello"); string s2("abcdef"); string::iterator p = s.begin(); //迭代器p s.insert(p,'A'); //在迭代器p指向的s開始之前插入A cout << s << endl; //s為Ahello s.insert(p,3,'B'); //p指向返回的Ahello的A處,在A之前插入3個B cout << s << endl; //s為BBBAhello string::iterator b = s2.begin(); //迭代器b string::iterator e = s2.end(); //迭代器e //p = s.begin(); //p指向s s.insert(p,b,e); //在p指向的s之前插入b和e迭代器范圍內的元素abcdef cout << s << endl; //s為abcdefBBBAhello s = "hello"; cout << s << endl; //s為hello s.assign(b,e); //s所有的元素倍替換為b到e之間的元素,b與e之間為s2 cout << s << endl; //s為abcdef s.assign(8,'K'); cout << s << endl; //s為KKKKKKKK p = s2.begin(); //迭代器p指向s2的a s2.erase(p); //刪除迭代器p指向的元素a cout << s2 << endl; //s2為bcdef p = s2.begin(); //a被刪除,p指向b p++; //指向c p++; //指向d string::iterator p2 = s2.end(); //p2迭代器指向f p2--; //指向e s2.erase(p,p2); //刪除p指向的d和p2指向的e之間的元素 cout << s2 << endl; //s2為bcf return 0; }
運行結果:
運行結果
string 類型特有的版本:
string以數組的形式存儲,可以用數組的下標進行修改操作:
![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZNqAVDKlAABnSxGUdt8325.png)
代碼實例:
#include #include using namespace std; //2020.05。27 測試字符串操作 公眾號:C語言與CPP編程 int main() { string s("hello"); string s2("abc"); s.insert(0,3,'A'); //在s下標是0之前插入3個A cout << s << endl; //s為AAAhello s.insert(5,s2); //在AAAhello下標是5的元素之前插入abc cout << s << endl; //s為AAAheabcllo s2 = "123456"; s.insert(0,s2,2,3); //在s的下標是0之前插入s2下標為2開始往后的3個元素345 cout << s << endl; //s為345AAAheabcllo char *cp = "Stately plup Buck"; s.assign (cp,7); cout << s << endl; //s為Stately s.assign(cp); //沒有長度,默認是拷貝全部 cout << s << endl; //s為Stately plup Buck s = "hello"; s.insert (0,cp,7); cout << s <
運行結果:
![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZXKASpzoAAAKaZdcNPM581.png)
運行結果
3 適合string類型操作的函數
substr()主要功能是復制子字符串,要求從指定位置開始,并具有指定的長度。
append() 方法在被選元素的結尾(仍然在內部)插入指定內容。提示:如需在被選元素的開頭插入內容,請使用prepend()方法。
replace() 該函數返回一個字符串,其中指定的字符串已經被替換為另一字符串,并且替換的次數也可以指定。
代碼實例:
#include #include using namespace std; //2020.05.27 測試字符串操作 公眾號:C語言與CPP編程 int main() { string s("Hello world"); string s2 = s.substr(6,5); //從第6個開始取5個 cout << s2 << endl ; //s2為world s2 = s.substr(6); //從第6個開始取拷貝所有的 cout << s2 << endl ; //s2為world s2 = s.substr(6); //s2拷貝s的全部,相當于s2=s cout << s2 << endl ; //s2為Hello world s = "C++ Primer"; s.append(" 3rd Ed"); //再s最后添加3rd Ed cout << s<< endl ; //s為C++ Primer 3rd Ed s = "C++ Primer"; s.insert(s.size()," 3rd Ed"); //最后插入 cout << s<< endl ; //s為C++ Primer 3rd Ed s.replace(11,3,"4th"); //下標11開始3個替換4th cout << s<< endl ; //s為C++ Primer 4th Ed s.replace(11,3,"Fourth"); //下標11開始3個替換Fourth cout << s<< endl ; //s為C++ Primer Fourth Ed s = "C++ Primer 3rd Ed"; //replace相當于先刪除后插入 s.erase (11,3); //刪除3rd s.insert(11,"Fourth"); //插入Fourth cout << s<< endl ; //s為C++ Primer Fourth Ed return 0; }
運行結果:
![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZXaARC2FAAAKWn5_9xk371.png)
運行結果
4 string類型的查找
![](http://file.elecfans.com/web1/M00/C7/86/pIYBAF9oZPSAUa9uAAA60_jbewM754.png)
代碼實例:
#include #include using namespace std; //2020.05.27 測試字符串操作 公眾號:C語言與CPP編程 int main() { string name("AnnaBelle"); string::size_type pos1 = name.find("Bell"); cout << pos1 << endl; //返回下標4,如果沒找到返回npos if(pos1 == string::npos) cout << "沒找到!" << endl; else cout << "找到了!下標:" << pos1 <
運行結果:
![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZXeATe3tAAAIMorYtVY782.png)
運行結果
5 string對象的比較
![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZRCAWRioAAAuhaHtmBg557.png)
代碼實例:
#include #include #include using std::cout; using std::endl; using std::cin; using std::string; int main(void) { string str1="hi,test,hello"; string str2="hi,test"; //字符串比較 if(str1.compare(str2)>0) printf("str1>str2 "); else if(str1.compare(str2)<0) printf("str1
運行結果:
![](http://file.elecfans.com/web1/M00/C7/0C/o4YBAF9oZXeAJfUkAAAL3gweYc8578.png)
運行結果
評論