C 程式設計入門 上 string類的基本用法

2022-08-16 01:30:19 字數 3661 閱讀 6435

string類中的函式

1. 構造

2. 追加

3. 賦值

4. 位置與清除

5. 長度與容量

6. 比較

7. 子串

8. 搜尋

9. 運算子

追加字串

string s1("

welcome");

"to c++

"); //

cout << s1 << endl; //

s1 now becomes welcome to c++

string s2("

welcome");

"to c and c++

", 3, 2); //

cout << s2 << endl; //

s2 now becomes welcome c

string s3("

welcome");

"to c and c++

", 5); //

cout << s3 << endl; //

s3 now becomes welcome to c

string s4("

welcome");

4, '

g'); //

cout << s4 << endl; //

s4 now becomes welcomegggg

為字串賦值

string s1("

welcome

");

s1.assign(

"dallas

"); //

assigns "dallas" to s1

cout << s1 << endl; //

s1 now becomes dallas

string s2("

welcome

");

s2.assign(

"dallas, texas

", 1, 3); //

assigns "all" to s2

cout << s2 << endl; //

s2 now becomes all

string s3("

welcome

");

s3.assign(

"dallas, texas

", 6); //

assigns "dallas" to s3

cout << s3 << endl; //

s3 now becomes dallas

string s4("

welcome");

s4.assign(

4, '

g'); //

assigns "gggg" to s4

cout << s4 << endl; //

s4 now becomes gggg

at(index):  返回當前字串中index位置的字元

clear(): 清空字串

erase(index, n):  刪除字串從index開始的n個字元

empty(): 檢測字串是否為

string s1("

welcome");

cout

<< s1.at(3) << endl; //

s1.at(3) returns c

cout << s1.erase(2, 3) << endl; //

s1 is now weme

s1.clear();

//s1 is now empty

cout << s1.empty() << endl; //

s1.empty returns 1 (means true)

比較字串:

string s1("

welcome");

string s2("

welcomg");

cout

<< s1.compare(s2) << endl; //

returns -2

cout << s2.compare(s1) << endl; //

returns 2

cout << s1.compare("

welcome

") << endl; //

returns 0

獲取子串:

at() 函式用於獲取乙個單獨的字元;而substr() 函式則可以獲取乙個子串

string s1("

welcome");

cout

<< s1.substr(0, 1) << endl; //

returns w ; 從 0 號位置開始的 1 個字元

cout << s1.substr(3) << endl; //

returns come ; 從 3 號位置直到末尾的子串

cout << s1.substr(3, 3) << endl; //

returns com ;從 3 號位置開始的 3 個字元

搜尋字串

string s1("

welcome to html

"cout

<< s1.find("

co") << endl; //

returns 3 ; 返回子串出現的第乙個位置);

cout << s1.find("

co", 6) << endl; //

returns -1 從 6 號位置開始查詢子串出現的第乙個位置

cout << s1.find('

o') << endl; //

returns 4 返回字元出現的第乙個位置

cout << s1.find('

o', 6) << endl; //

returns 9 從 6 號位置開始查詢字元出現的第乙個位置

插入和替換字串

insert() : 將某個字元/字串插入到當前字串的某個位置

replace() 將本字串從某個位置開始的一些字元替換為其它內容

string s1("

welcome to html

");

s1.insert(

11, "

c++ and

");

cout

<< s1 << endl; //

s1 becomes welcome to c++ and html

string s2("aa"

); s2.insert(

1, 4, '

b'); //

在 1 號位置處連續插入 4 個相同字元

cout << s2 << endl; //

s2 becomes to abbbba

string s3("

welcome to html

");

s3.replace(

11, 4, "

c++"); //

從 11 號位置開始向後的 4 個字元替換掉。注意 '\0'

cout << s3 << endl; //

returns welcome to c++

C 程式設計入門 上 之物件和類

物件導向程式設計 如何定義物件?同型別物件用一 個通用的類來定義 class c c ca,cb 乙個類用變數來定義資料域,用函式定義行為。class cirle cirle double newr double get 建構函式 類中有 一種特殊的 建構函式 在建立物件時被自動呼叫。通常用來初始化...

虛基類的簡單應用 C 程式設計

c 編譯系統只執行最後的派生類對虛基類的建構函式的呼叫,而忽略虛基類的其他派生類對虛基類的建構函式的呼叫,這就保證了虛基類的資料成員不會被多次初始化。多重繼承派生類 虛基類 include include include using namespace std class person 基類 人 p...

C 程式設計入門之五(string容器)

1.本質 string是c 風格的字串,而string本質上是乙個類 2.string和char 區別 char 是乙個指標 string是乙個類,類內部封裝了char 管理這個字串,是乙個char型的容器。3.特點 string類內部封裝了很多成員方法 例如 查詢find,拷貝copy,刪除del...