自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++字符串替換函數(shù)具體實(shí)現(xiàn)方法詳解

開發(fā) 后端
我們?cè)趯?shí)際編程中,如果沒有現(xiàn)成的函數(shù),可以自行編寫一個(gè)C++函數(shù)來實(shí)現(xiàn)具體的功能。今天就為大家詳細(xì)介紹一下C++字符串替換函數(shù)的實(shí)現(xiàn)方式。

C++編程語言應(yīng)方式靈活,一個(gè)功能的實(shí)現(xiàn)可以采用多種方式來實(shí)現(xiàn)。比如對(duì)于字符串替換就可以有很多方法來實(shí)現(xiàn),這里主要說一下STL里的WString中的替換,雖然WString自帶了一個(gè)Replace函數(shù),但是只能替換一次,太不好了,因此單獨(dú)寫了個(gè)C++字符串替換函數(shù)

C++字符串替換函數(shù)代碼如下:

  1. @brief 實(shí)現(xiàn)字符串替換   
  2. @param orignStr 源串   
  3. @param oldStr 查找的串   
  4. @param newStr 替換的新串   
  5. @return 返回修改后的串  
  1. static wstring Replace(const wstring& orignStr, 
    const wstring& oldStr, const wstring& newStr); 

[C++字符串替換函數(shù)的實(shí)現(xiàn)]

  1. std::wstring Replace( const wstring& orignStr, const 
    wstring& oldStr, const wstring& newStr )   
  2. {   
  3. size_t pos = 0;   
  4. wstring tempStr = orignStr;   
  5. wstring::size_type newStrnewStrLen = newStr.length();   
  6. wstring::size_type oldStroldStrLen = oldStr.length();   
  7. while(true)   
  8. {   
  9. pos = tempStr.find(oldStr, pos);   
  10. if (pos == wstring::npos) break;   
  11. tempStr.replace(pos, oldStrLen, newStr);   
  12. pos += newStrLen;  
  13. }   
  14. return tempStr;   

以上就是我們對(duì)C++字符串替換函數(shù)的相關(guān)介紹。

【編輯推薦】

  1. C++操作剪貼板具體應(yīng)用方式解析
  2. C++構(gòu)造函數(shù)相關(guān)使用方式解讀
  3. C++友元函數(shù)基本應(yīng)用方式解析
  4. C++連接MySqL數(shù)據(jù)庫操作代碼解讀
  5. C++ switch-case語句應(yīng)用經(jīng)驗(yàn)講解
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-04 10:52:36

C++字符串分割函數(shù)

2010-02-04 11:23:25

C++反射機(jī)制

2010-11-26 10:14:40

MySQL repla

2010-02-01 17:02:53

C++產(chǎn)生隨機(jī)數(shù)

2024-02-22 09:46:04

C++字符串格式化開發(fā)

2009-11-26 13:52:07

PHP字符串替換函數(shù)s

2010-02-06 11:19:33

C++獲取文件

2009-10-13 16:09:27

.NET字符串解析

2009-08-06 16:01:09

C#字符串函數(shù)大全

2021-09-07 09:23:07

C++字符串算法

2010-02-02 11:27:16

C++字符串

2021-08-20 06:58:31

C++Python函數(shù)

2009-11-30 18:26:06

PHP字符串替換

2009-11-26 16:43:11

PHP截取中文字符串

2010-02-02 16:23:46

C++實(shí)現(xiàn)WPF動(dòng)畫

2011-08-10 11:08:32

Objective-C字符串NSString

2011-07-15 11:07:41

C語言字符串函數(shù)

2011-07-15 12:41:53

C語言

2010-02-03 13:26:53

C++計(jì)時(shí)

2010-03-05 16:56:42

Python綁定C++
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)