首页 > 技术文章 > std::string 字符串替换

tyche116 2018-09-11 10:59 原文

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 }

 

推荐阅读