使用新字串取代舊字串

使用新字串取代舊字串

   1: namespace StringPlus
   2: {
   3:     /// Replace all old strings by the new string in the source string.
   4:     std::string Replace(const std::string& szSrc,const std::string& szOld,const std::string& szNew)
   5:     {
   6:         if(szNew.find(szOld)!=std::string::npos)
   7:             throw "unlimited recurrence";
   8:  
   9:         std::string _szBuff(szSrc);
  10:  
  11:         int _oldLength    = szOld.size();
  12:         int _found        = _szBuff.find(szOld);
  13:  
  14:         while( _found!=std::string::npos )
  15:         {
  16:             _szBuff.replace(_found, _oldLength, szNew);
  17:             _found = _szBuff.find(szOld);
  18:         }    
  19:  
  20:         return _szBuff;
  21:     }
  22: }