C++ string字串的增刪改查
c++ 提供的string類包含了若干實用的成員函式,大大方便了字串的增加、刪除、更改、查詢等操作。
插入字串
insert()函式可以在string字串中置頂的位置插入另一個字串,它的原型為:
string& insert (size_t pos, const string& str);
- 1
看這個插入的格式我們就能猜想到,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");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
執行結果:
12345aaa67890
12345bbb67890
請按任意鍵繼續. . .
- 1
- 2
- 3
- 4
insert()函式的第一個引數有越界的可能,如果越界,則會產生執行時異常。我惡魔你要捕獲這個異常。
刪除字串
erase()函式可以刪除string變數中的一個字串,原型為:
string& erase (size_t pos = 0, size_t len = npos);
- 1
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");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
執行結果:
1234567890
12345
1234590
請按任意鍵繼續. . .
- 1
- 2
- 3
- 4
在 pos 引數沒有越界的情況下, len 引數也可能會導致要刪除的子字串越界。但實際上這種情況不會發生,erase() 函式會從以下兩個值中取出最小的一個作為待刪除子字串的長度:
- len的值
- 字串長度減去 pos 的值。
簡單的說,就是待刪除字串最多隻能刪除到字串結尾。
提取字串
substr()函式原型為:
string substr (size_t pos = 0, size_t len = npos) const;
- 1
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");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
輸出結果為:
first second third
second
請按任意鍵繼續. . .
- 1
- 2
- 3
- 4
系統對 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;
- 1
- 2
第一個引數的表示為待查詢的子字串,它可以是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");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
輸出結果為:
6
找到的索引:0
找到的索引:13
2
請按任意鍵繼續. . .
- 1
- 2
- 3
- 4
- 5
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");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
執行的結果為:
Found at index : 6
請按任意鍵繼續. . .
- 1
- 2
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
執行結果為:
Found at index : 3
- 1
s1 和 s2 共同具有的字元是 ’s’,該字元在 s1 中首次出現的下標是3,故查詢結果返回3。