淺談C++中replace()方法
本文主要針對(duì)c++中常用replace函數(shù)用法給出九個(gè)樣例程序:
用法一:
/* *用str替換指定字符串從起始位置pos開始長(zhǎng)度為len的字符 *string& replace (size_t pos, size_t len, const string& str); */ int main() { string line = "this@ is@ a test string!"; line = line.replace(line.find("@"), 1, ""); //從第一個(gè)@位置替換第一個(gè)@為空 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法二:
/* *用str替換 迭代器起始位置 和 結(jié)束位置 的字符 *string& replace (const_iterator i1, const_iterator i2, const string& str); */ int main() { string line = "this@ is@ a test string!"; line = line.replace(line.begin(), line.begin()+6, ""); //用str替換從begin位置開始的6個(gè)字符 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法三:
/* *用substr的指定子串(給定起始位置和長(zhǎng)度)替換從指定位置上的字符串 *string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); */ int main() { string line = "this@ is@ a test string!"; string substr = "12345"; line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(從1位置數(shù)共3個(gè)字符)替換從0到5位置上的line cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法四:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/* *用str替換從指定位置0開始長(zhǎng)度為5的字符串 *string& replace(size_t pos, size_t len, const char* s); */ int main() { string line = "this@ is@ a test string!"; char* str = "12345"; line = line.replace(0, 5, str); //用str替換從指定位置0開始長(zhǎng)度為5的字符串 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法五:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/* *用str替換從指定迭代器位置的字符串 *string& replace (const_iterator i1, const_iterator i2, const char* s); */ int main() { string line = "this@ is@ a test string!"; char* str = "12345"; line = line.replace(line.begin(), line.begin()+9, str); //用str替換從指定迭代器位置的字符串 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法六:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/* *用s的前n個(gè)字符替換從開始位置pos長(zhǎng)度為len的字符串 *string& replace(size_t pos, size_t len, const char* s, size_t n); */ int main() { string line = "this@ is@ a test string!"; char* str = "12345"; line = line.replace(0, 9, str, 4); //用str的前4個(gè)字符替換從0位置開始長(zhǎng)度為9的字符串 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法七:string轉(zhuǎn)char*時(shí)編譯器可能會(huì)報(bào)出警告,不建議這樣做
/* *用s的前n個(gè)字符替換指定迭代器位置(從i1到i2)的字符串 *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); */ int main() { string line = "this@ is@ a test string!"; char* str = "12345"; line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4個(gè)字符替換指定迭代器位置的字符串 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法八:
/* *用重復(fù)n次的c字符替換從指定位置pos長(zhǎng)度為len的內(nèi)容 *string& replace (size_t pos, size_t len, size_t n, char c); */ int main() { string line = "this@ is@ a test string!"; char c = '1'; line = line.replace(0, 9, 3, c); //用重復(fù)3次的c字符替換從指定位置0長(zhǎng)度為9的內(nèi)容 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
用法九:
/* *用重復(fù)n次的c字符替換從指定迭代器位置(從i1開始到結(jié)束)的內(nèi)容 *string& replace (const_iterator i1, const_iterator i2, size_t n, char c); */ int main() { string line = "this@ is@ a test string!"; char c = '1'; line = line.replace(line.begin(), line.begin()+9, 3, c); //用重復(fù)3次的c字符替換從指定迭代器位置的內(nèi)容 cout << line << endl; return 0; }
運(yùn)行結(jié)果:
注:所有使用迭代器類型的參數(shù)不限于string類型,可以為vector、list等其他類型迭代器。
相關(guān)文章
Qt模仿實(shí)現(xiàn)文字浮動(dòng)字母的效果
這篇文章主要介紹了通過Qt實(shí)現(xiàn)的文字浮動(dòng)的效果,效果很簡(jiǎn)單就是文本向上移動(dòng),在移動(dòng)過程中文字整體變大或縮小。感興趣的可以試一試2022-01-01C++使用cjson操作Json格式文件(創(chuàng)建、插入、解析、修改、刪除)
本文主要介紹了C++使用cjson操作Json格式文件(創(chuàng)建、插入、解析、修改、刪除),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C語言斷言函數(shù)assert()的學(xué)習(xí)筆記
在C語言庫(kù)函數(shù)中提供了一個(gè)輔助調(diào)試程序的小型庫(kù),它是由assert()宏組成,本文就詳細(xì)的介紹了一下如何使用,感興趣的可以了解一下2021-11-11C++實(shí)現(xiàn)稀疏矩陣的壓縮存儲(chǔ)實(shí)例
本篇文章主要介紹了C++實(shí)現(xiàn)稀疏矩陣的壓縮存儲(chǔ)實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06C語言連續(xù)生成多個(gè)隨機(jī)數(shù)實(shí)現(xiàn)可限制范圍
這篇文章主要介紹了C語言連續(xù)生成多個(gè)隨機(jī)數(shù)實(shí)現(xiàn)可限制范圍,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01