c 字串學習筆記

2022-09-10 05:18:08 字數 3983 閱讀 9734

#

include

#include

using

namespace std;

string str1;

//生成空字串

cin << str1;

cout << str1;

string str2

("hello");

//生成並初始化

cout << str2;

string str3

(str2)

;//hello

cout << str3;

string str4(10

,'h');

//hhhhhhhhhh

cout << str4;

char a=

"hello"

;//hello

string str5

(a);

cout << str5;

string str

("hello");

char sz=

"word"

;string str6

("word");

str.(10

,'w');

//追加單個字元

str.

(sz)

;//追加c風格字串

str.

(str6)

;//追加字串

返回值:0、1、-1

前 > 後 返回1、前 < 後 返回-1、前 = 後 返回0;

string date1

("21");

string date2

("22");

string date3

("22");

char date4=

"23"

;cout << date1.

compare

(date2)

<< endl;

//-1

cout << date2.

compare

(date1)

<< endl;

//1cout << date2.

compare

(date3)

<< endl;

//0cout << date3.

compare

(date4)

<< endl;

//-1

string findstr

("abcdef");

cout << findstr.

find

('d',2

)<< findstr.

find

('d',5

);//第乙個引數是要查詢的字元,第二個引數是起始查詢的位置。返回值為該字元在字串中的位置int(0位起),若無則返回-1.

char findchar =

"bcd"

;cout << findstr.

find

(findchar,0)

;//返回字串在string中的位置,本題為1

string findstr1

("abc");

cout << findstr.

find

(findstr1,0)

;

string replacestr

("abcdefg");

cout << replacestr.

replace(2

,5,"2345");

//先刪除c-e,在插入2345. ab2345fg。

cout << replacestr.

replace(2

,5,10

,'m');

//先刪除,再插入10個m

string incertstr

("123456");

string incertstr1

("abcdef");

cout << incertstr.

incert(3

, incertstr1)

;

string str_test

("abcdefg");

str_test.

erase(3

,3);

//從d起 刪除3個字元

str_test.

substr(3

,3);

//返回string, 提取def。

獲取字串長度str.length()

注意

再進行多次轉換的時候,必須呼叫stringstream的成員函式clear().

clear()重置流的標誌狀態;str()清空流的記憶體緩衝,重複使用記憶體消耗不再增加!

在多次資料型別轉換的場景下,必須使用clear()方法清空stringstream,不使用clear()方法或使用str("")方法,都不能得到資料型別轉換的正確結果。

#

include

stringstream stream;

stream.

clear()

;stream.

str("")

;

用法1 資料型別的轉換

// int -> string

stringstream sstream;

string strresult;

int nvalue =

1000

;// 將int型別的值放入輸入流中

sstream << nvalue;

// 從sstream中抽取前面插入的int型別的值,賦給string型別

sstream >> strresult;

用法2 多字串的拼接

stringstream sstream;

// 將多個字串放入 sstream 中

sstream <<

"first"

<<

" "<<

"string,"

;sstream <<

" second string"

;// 可以使用 str() 方法,將 stringstream 型別轉換為 string 型別;

cout <<

"strresult is: "

<< sstream.

str(

)<< endl;

用法3 可以用於分割被空格、製表符等符號分割的字串

#

include

#include

//istringstream 必須包含這個標頭檔案

#include

using

namespace std;

intmain()

}

用法4 實現任意型別的轉換

template

<

typename

out_type

,typename

in_value

>

out_type convert

(const in_value & t)

intmain()

return0;

}

C 學習筆記 字串

字串 char型別的唯讀陣列 1 常用方法 length 獲得字串中字元的個數 toupper 將字串轉換為大寫 tolower 將字串轉換為小寫 equals 比較兩個字串是否相同,equals string a,stringcomparison.ordinalignorecase 比較時可以忽略...

字串 C 學習筆記之 字串和字串流

字元陣列,也就是存放字元型別資料的陣列,只不過字元陣列的結尾必須是 0 c 已經提供了一些字串處理函式,這些函式被封裝在標頭檔案和 中。此外,為了更方便地對字串進行操作,c 中定義了乙個 string 類,可以在使用的時候包含標頭檔案。此外,可以用乙個字串類變數或者字元陣列或者字元直接對字串類變數進...

C 字串學習筆記二

續集來了,快樂分享。1.字串連線時最好保證字串中不發生變化的部分宣告為常量。對於單一的連線,不要使用stringbuilder,因為建立物件所耗費的系統開銷會超出連線所帶來的效能上的收益。對於單一的連線,可能的話,應該使用常量和其他的內建字串,因為這樣可以將效能提高3倍。2.如何分解句子到單詞 st...