Originariamente inviato da MItaly
codice:
//Replaces all the instances of search with replace in string; returns the number of substitutions done
unsigned int ReplaceString(std::string & string,const std::string & search,const std::string & replace)
{
	unsigned int ret=0;
	for(std::string::size_type pos=string.find(search);pos!=string.npos;ret++,pos=string.find(search,++pos))
	{
		if(search.length()>replace.length())
			string.erase(pos,search.length()-replace.length());
		if(search.length()<replace.length())
			string.insert(pos,replace.length()-search.length(),' ');
		string.replace(pos,replace.length(),replace);
		pos+=replace.length();
	}
	return ret;
}
A cosa serve usare erase e insert, prima del replace ? Mi sfugge qualcosa ? :master:
Io uso questo codice:
codice:
int buffReplace(       std::string &buffer,
                 const std::string &strFind,
                 const std::string &strRepl )
{
   int    count = 0;
   size_t pos   = 0;
   if( 0==buffer.size()
	 || 0==strFind.size() )
		return -1;
   while(( pos = buffer.find( strFind, pos ))!=std::string::npos )
   {
		buffer.replace( pos, strFind.size(), strRepl.c_str() );
		pos += strRepl.size();
		count++;
   }
   return count;
}
Magari troviamo un baco anche nel mio codice