1. 程式人生 > 其它 >string所有函式詳解——史上最全,最貼心

string所有函式詳解——史上最全,最貼心

技術標籤:string字串標頭檔案


簡介

區別於#include<string.h>標頭檔案,#include<string>標頭檔案中的函式專門用於處理C++中string字串。而前者是用來處理C語言中char陣列的標頭檔案。


常用函式

一、運算子: +、=、[]

1、在C++中,string過載了+、=、和[],可以讓我們十分方便的對字串進行運算。

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "123"
, s2 = "567"; string s3 = s1 + s2; cout << s3 << endl; //實現等號賦值, 加號串聯 cout << s3[0] << endl; //實現對字串中某個值的訪問 return 0; }

輸出:
123567
1



二、求長度:length()、size()

二者都可以用來求長度。length()為獲取字串長度。 size()為獲取字串這個容器中元素的個數

#include<iostream>
#include<string> 
using namespace
std; int main() { string s1 = "123"; cout << s1.length() << endl; cout << s1.size() << endl; return 0; }

輸出:
3
3



三、翻轉字串:reverse()

它本是#include<algorithm>標頭檔案中的函式, 但在字串中應用非常廣泛,因此我選擇把它加入string標頭檔案中講解。

1、實現對全部字串的翻轉。

#include<iostream>
#include<algorithm>
#include<string> using namespace std; int main() { string s1 = "12345"; reverse(s1.begin(), s1.end()); cout << s1 << endl; return 0; }

輸出:
54321


2、通過使用迭代器, 還可以實現對指定位置的翻轉。

#include<iostream>
#include<string> 
#include<algorithm>
using namespace std;
int main() {
	string s1 = "12345";
	string::iterator it = s1.begin();
	reverse(it, it+3);
	cout << s1 << endl;
	return 0;
}

輸出:
32145



四、判斷字串是否為空:empty()

如果為空,則值為1,反之為0

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "12345";
	if(s1.empty() == 1) cout << "字串為空";
	else cout << "字串非空";
	return 0;
}

輸出:
字串非空



五、字串清空函式:clear()

將字串清空

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "12345";
	s1.clear();
	if(s1.empty() == 1) cout << "字串為空";
	else cout << "字串非空";
	return 0;
}

輸出:
字串為空



六、刪除字串:erase()

用法為:erase(int x, int num),從x位置向後刪除(不包括x),刪除num個字元。

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "12345";
	s1.erase(0, 2);		//從0的位置向後刪,刪除2個
	cout << s1; 
	return 0;
}

輸出:
345


七、插入函式:insert()

用法為:insert(int x1, int num, char c),從x位置向後插入(不包括x),插入num個字元c。

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "12345";
	s1.insert(0, 1, '1');
	cout << s1; 
	return 0;
}

輸出:
112345



八、查詢函式之find()、rfind()、find_first_of()、find_first_not_of()、 find_last_of()、find_last_not_of()

8.1 find()函式

1、查詢指定字元, 從某個位置向後開始查詢, 返回該位置,若查詢不到,則返回-1

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "123145";
	int find1 = s1.find('1', 1); 
	cout << find1 << endl; 
	
	int find2 = s1.find('6');
	cout << find2;
	return 0;
}

輸出:
3
-1


2、查詢字串, 原理同上, 若查詢到返回字串的起始位置(注意是起始位置)。

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "123145";
	int find1 = s1.find("23", 0); 
	cout << find1 << endl; 
	
	int find2 = s1.find("56", 0);
	cout << find2;
	return 0;
}

輸出:
1
-1


8.2 rfind()函式

從後往前查詢指定的字元或字串,用法同上。


8.3 find_first_of()函式

查詢第一個等於x字元的位置。 用法同上


8.4 find_first_not_of()函式

查詢第一個不等於x字元的位置。 用法同上


8.5 find_last_of()函式

查詢最後一個等於x字元的位置(注意是查詢最後一個,而不是從後往前查詢)。


8.6 find_last_not_of()函式

查詢最後一個不等於x字元的位置。 用法同上

注意:只有find和rfind函式可以查詢字串!其他函式查詢會出現值不準確的情況!



九、替換函式:replace()

把0後的兩個字元替換成A

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "12345";
	s1.replace(0, 2, "A");
	cout << s1;
	return 0;
}

輸出:
A345



十、將string轉換成char*的函式: c_str()

在做題過程中, printf往往比cout要靈活一些,尤其是在輸出指定內容時, 但我們知道,printf無法輸出string型字串,我們就可以用c_str()函式進行轉化後輸出。

#include<iostream>
#include<string> 
using namespace std;
int main() {
	string s1 = "12345";
	
	printf("%s", s1.c_str());
	return 0;
}

輸出:
12345



十一、佛祖保佑,永無BUG氾濫

/*                         _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                           O\ = /O
                       ____/`---'\____
                     .   ' \\| |// `.
                      / \\||| : |||// \
                    / _||||| -:- |||||- \
                      | | \\\ - /// | |
                    | \_| ''\---/'' | |
                     \ .-\__ `-` ___/-. /
                  ___`. .' /--.--\ `. . __
               ."" '< `.___\_<|>_/___.' >'"".
              | | : `- \`.;`\ _ /`;.`/ - ` : | |
                \ \ `-. \_ __\ /__ _/ .-` / /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
 
        .............................................
                 佛祖鎮樓                  BUG辟易
         佛曰:
                 寫字樓裡寫字間,寫字間里程序員;
                 程式人員寫程式,又拿程式換酒錢。
                 酒醒只在網上坐,酒醉還來網下眠;
                 酒醉酒醒日復日,網上網下年復年。
                 但願老死電腦間,不願鞠躬老闆前;
                 賓士寶馬貴者趣,公交自行程式設計師。
                 別人笑我忒瘋癲,我笑自己命太賤;
                 不見滿街漂亮妹,哪個歸得程式設計師?
 */
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
 O\ = /O
___/`---'\____
.   ' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
         .............................................
          佛曰:bug氾濫,我已癱瘓!
 */


如果哪裡有困惑,歡迎給筆者留言。

如果這篇博文對你產生了幫助,可以留下小小的一個贊哦,大家的支援是我更新的最大動力~