用 string來(lái)代替char * 數(shù)組,使用sort排序算法來(lái)排序,用unique 函數(shù)來(lái)去重 1、Define ??????? string s1 = "hello"; ??????? string s2 = "world"; ??????? string s3 = s1 + "," + s2 +"!/n"; 2、append ??????? s1 += ",shanshan/n"; 3、Compare ??????? if(s1 == s2) ?????????? ..... ??????? else if(s1 == "hello") ?????????? ..... 4、 string 重載了許多操作符,包括 +, +=, <, =, , [], <<, >>等,正式這些操作符,對(duì)字符串操作非常方便 #include #include using namespace std; int main(){ string strinfo="Please input your name:"; cout << strinfo ; cin >> strinfo; if( strinfo == "winter" ) cout << "you are winter!"< else if( strinfo != "wende" ) cout << "you are not wende!"< else if( strinfo < "winter") cout << "your name should be ahead of winter"< else cout << "your name should be after of winter"< strinfo += " , Welcome to China!"; cout << strinfo< cout <<"Your name is :"< string strtmp = "How are you? " + strinfo; for(int i = 0 ; i < strtmp.size(); i ++) cout< return 0; } 5、find函數(shù) 由于查找是使用最為頻繁的功能之一,string 提供了非常豐富的查找函數(shù)。其列表如下: | 函數(shù)名 | 描述 | | find | 查找 | | rfind | 反向查找 | | find_first_of | 查找包含子串中的任何字符,返回第一個(gè)位置 | | find_first_not_of | 查找不包含子串中的任何字符,返回第一個(gè)位置 | | find_last_of | 查找包含子串中的任何字符,返回最后一個(gè)位置 | | find_last_not_of | 查找不包含子串中的任何字符,返回最后一個(gè)位置 | 以上函數(shù)都是被重載了4次,以下是以find_first_of 函數(shù)為例說(shuō)明他們的參數(shù),其他函數(shù)和其參數(shù)一樣,也就是說(shuō)總共有24個(gè)函數(shù): size_type find_first_of(const basic_string& s, size_type pos = 0) size_type find_first_of(const charT* s, size_type pos, size_type n) size_type find_first_of(const charT* s, size_type pos = 0) size_type find_first_of(charT c, size_type pos = 0)
所有的查找函數(shù)都返回一個(gè)size_type類(lèi)型,這個(gè)返回值一般都是所找到字符串的位置,如果沒(méi)有找到,則返回string::npos。 其實(shí)string::npos表示的是-1。即沒(méi)找到就返回-1。例子如下: #include #include using namespace std; int main(){ string strinfo=" //*---Hello Word!......------"; string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int first = strinfo.find_first_of(strset); if(first == string::npos) { cout<<"not find any characters"< return -1; } int last = strinfo.find_last_of(strset); if(last == string::npos) { cout<<"not find any characters"< return -1; } cout << strinfo.substr(first, last - first + 1)< return 0; } 6、insert函數(shù), replace函數(shù)和erase函數(shù)
string只是提供了按照位置和區(qū)間的replace函數(shù),而不能用一個(gè)string字串來(lái)替換指定string中的另一個(gè)字串。 例子: #include #include using namespace std; int main() { string strinfo="This is Winter, Winter is a programmer. Do you know Winter?"; cout<<"Orign string is :/n"< string_replace(strinfo, "Winter", "wende"); cout<<"After replace Winter with wende, the string is :/n"< return 0; }
string.erase(pos,srclen);//srclen是刪除的長(zhǎng)度 string.insert(pos,strdst); //pos是定位,strdst是插入的函數(shù) void string_replace(string & strBig, const string & strsrc, const string &strdst) { string::size_type pos=0; string::size_type srclen=strsrc.size(); string::size_type dstlen=strdst.size(); while( (pos=strBig.find(strsrc, pos)) != string::npos){ strBig.erase(pos, srclen); strBig.insert(pos, strdst); pos += dstlen; } }
相關(guān)鏈接:http://www.stlchina.org/twiki/bin/view.pl/Main/STLDetailString ? 7、切割字符串 #include #include #include using namespace std; int main() { string text = "big|dog|china|sonic|free"; stringstream ss(text); string sub_str; while(getline(ss,sub_str,'|')) //以|為間隔分割test的內(nèi)容 cout << sub_str << endl;
return 0; } 輸出如下: big dog china sonic free
8、構(gòu)造函數(shù)和析構(gòu)函數(shù) string s 生成一個(gè)空字符串S string s(str) Copy構(gòu)造函數(shù),生成字符串Str的一個(gè)復(fù)制品 string s(str,stridx) 將字符串Str內(nèi)始于位置Stridx的部分,當(dāng)作字符串S的初值 string s(str,stridx,strlen) 將字符串Str內(nèi)始于位置Stridx且長(zhǎng)度為strlen的部分,當(dāng)作字符串S的初值 string s(cstr)??????????? 以C-String cstr作為S的初值 string s(num,c)?????????? 生成一個(gè)字符串,包含Num個(gè)C字符 string s(beg,end)???????? 以區(qū)間[beg,end]內(nèi)的字符作為s初值 s.~string() ? ? ? ? ? ? ? 銷(xiāo)毀所有字符,釋放內(nèi)存 注意: std::string s('x');//error std::string s(1,'x'); //ok,create a string that has one charactor 'x'
9、substr(string.substr的方法) (1)參數(shù)
start:Number -- 一個(gè)整數(shù),指示 my_str 中用于創(chuàng)建子字符串的第一個(gè)字符的位置。如果 start 為一個(gè)負(fù)數(shù),則起始位置從字符串的結(jié)尾開(kāi)始確定,其中 -1 表示最后一個(gè)字符。
length:Number -- 要?jiǎng)?chuàng)建的子字符串中的字符數(shù)。如果沒(méi)有指定 length,則子字符串包括從字符串開(kāi)頭到字符串結(jié)尾的所有字符。
(2)返回
String -- 指定字符串的子字符串。
(3)示例
下面的示例創(chuàng)建一個(gè)新字符串 my_str,并使用 substr() 返回該字符串中的第二個(gè)單詞;首先,使用正的 start 參數(shù),然后使用負(fù)的 start 參數(shù): var my_str:String = new String("Hello world"); var mySubstring:String = new String(); mySubstring = my_str.substr(6,5); trace(mySubstring); // 輸出:world
mySubstring = my_str.substr(-5,5); trace(mySubstring); // 輸出:world
?
| ? | | |