std::string 字符串替換


std::string 沒有原生的字符串替換函數,需要自己來完成

 

 1 string& replace_str(string& str, const string& to_replaced, const string& newchars)     
 2 {
 3     for(string::size_type pos(0); pos != string::npos; pos += newchars.length())   
 4     {
 5         pos = str.find(to_replaced,pos);
 6         if(pos!=string::npos)     
 7            str.replace(pos,to_replaced.length(),newchars);     
 8         else  
 9             break;  
10     }     
11     return   str;     
12 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM