轉載請標明出處:
http://blog.csdn.net/u011974987/article/details/52505004
本文出自:【徐Xiho的博客】
c++ 提供的string類包含了若干實用的成員函數,大大方便了字符串的增加、刪除、更改、查詢等操作。
插入字符串
insert()函數可以在string字符串中置頂的位置插入另一個字符串,它的原型爲:
string& insert (size_t pos, const string& str);
看這個插入的格式我們就能猜想到,pos表示要插入的下標;str表示要插入的字符串,它可以是string變量,也可以是C風格的字符串。
看下面的代碼:
#include <iostream>
#include <string>
using namespace std;
void main(){
string s1, s2, s3;
s1 = s2 = "1234567890";
s3 = "aaa";
s1.insert(5, s3);
cout << s1 << endl;
s2.insert(5, "bbb");
cout << s2 << endl;
system("pause");
}
運行結果:
12345aaa67890
12345bbb67890
請按任意鍵繼續. . .
insert()函數的第一個參數有越界的可能,如果越界,則會產生運行時異常。我惡魔你要捕獲這個異常。
刪除字符串
erase()函數可以刪除string變量中的一個字符串,原型爲:
string& erase (size_t pos = 0, size_t len = npos);
pos 表示要刪除的子字符串的起始下標,len表示要刪除子字符串的長度。如果不指明len的話,那麼直接刪除pos到字符串結束處的所有字符(此時len =str.length-pos)。
示例代碼如下:
#include <iostream>
#include <string>
using namespace std;
void main(){
string s1, s2, s3;
s1 = s2 = s3 = "1234567890";
s2.erase(5);
s3.erase(5, 3);
cout<< s1 <<endl;
cout<< s2 <<endl;
cout<< s3 <<endl;
system("pause");
}
運行結果:
1234567890
12345
1234590
請按任意鍵繼續. . .
在 pos 參數沒有越界的情況下, len 參數也可能會導致要刪除的子字符串越界。但實際上這種情況不會發生,erase() 函數會從以下兩個值中取出最小的一個作爲待刪除子字符串的長度:
- len的值
- 字符串長度減去 pos 的值。
簡單的說,就是待刪除字符串最多隻能刪除到字符串結尾。
提取字符串
substr()函數原型爲:
string substr (size_t pos = 0, size_t len = npos) const;
pos爲要提取的子字符串的起始下標,len爲要提取的子字符串的長度。
#include <iostream>
#include <string>
using namespace std;
void main(){
string s1 = "first second third";
string s2;
s2 = s1.substr(6, 6);
cout << s1 << endl;
cout << s2 << endl;
system("pause");
}
輸出結果爲:
first second third
second
請按任意鍵繼續. . .
系統對 substr() 參數的處理和 erase() 類似:
- 如果 pos 越界,會拋出異常;
- 如果 len 越界,會提取從 pos 到字符串結尾處的所有字符。
字符串的查找
find()函數
find()函數用於string字符串中查找子字符串出現的位置,它的原型爲:
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
第一個參數的表示爲待查找的子字符串,它可以是string變量,也可以是C風格的字符串,第二個參數表示開始查找的位置(下標);
/字符串查找替換
void main()
{
string s1 = "apple google apple iphone";
//從0開始查找"google"的位置
int idx = s1.find("google", 0);
cout << idx << endl;
//統計apple出現的次數
int idx_app = s1.find("apple",0);
//npos大於任何有效下標的值
int num = 0;
while (idx_app != string::npos)
{
num++;
cout << "找到的索引:" << idx_app << endl;
idx_app+=5;
idx_app = s1.find("apple", idx_app);
}
cout << num << endl;
system("pause");
}
輸出結果爲:
6
找到的索引:0
找到的索引:13
2
請按任意鍵繼續. . .
find函數最終返回的是子字符串 第一次出現在字符串的其實下標,如果沒有查找到子字符串,那麼會返回一個無窮大的值 4294967295。統計apple出現的次數。先查找第一次出現的位置,接着
和npos大於任何有效下標的值,來判斷,while循環,每次加上自身的長度,最後統計出現的次數。。。
rfind()函數
rfind() 和 find() 很類似,同樣是在字符串中查找子字符串,不同的是 find() 函數從第二個參數開始往後查找,而 rfind() 函數則最多查找到第二個參數處,如果到了第二個參數所指定的下標還沒有找到子字符串,則返回一個無窮大值4294967295。
#include <iostream>
#include <string>
using namespace std;
void main(){
string s1 = "first second third";
string s2 = "second";
int index = s1.rfind(s2, 6);
if (index < s1.length())
cout << "Found at index : " << index << endl;
else
cout << "Not found" << endl;
system("pause");
}
運行的結果爲:
Found at index : 6
請按任意鍵繼續. . .
find_first_of() 函數
find_first_of() 函數用於查找子字符串和字符串共同具有的字符在再輔傳中首先出現的位置。
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "first second second third";
string s2 = "asecond";
int index = s1.find_first_of(s2);
if(index < s1.length())
cout<<"Found at index : "<< index <<endl;
else
cout<<"Not found"<<endl;
return 0;
}
運行結果爲:
Found at index : 3
s1 和 s2 共同具有的字符是 ’s’,該字符在 s1 中首次出現的下標是3,故查找結果返回3。
學習理解並整理下來的筆記。
希望大家能夠指點或提出寶貴意見,謝謝!一起學習。
轉載請註明出處:http://blog.csdn.net/u011974987/article/details/52505004
個人主頁:xuhaoblog.com