int replace_str(std::string& str, const char * oldpart, const char * newpart)
{
int nReplaced= 0;
std::string::size_type nIdx= 0;
std::string::size_type nOldLen= strlen(oldpart);
if ( 0 == nOldLen )
return 0;
static const char ch = 0x00;
std::string::size_type nNewLen= strlen(newpart);
const char* szRealNew= newpart == 0 ? &ch : newpart;
while ( (nIdx=str.find(oldpart, nIdx)) != std::string::npos )
{
str.replace(str.begin()+nIdx, str.begin()+nIdx+nOldLen, szRealNew);
nReplaced++;
nIdx += nNewLen;
}
return nReplaced;
{
int nReplaced= 0;
std::string::size_type nIdx= 0;
std::string::size_type nOldLen= strlen(oldpart);
if ( 0 == nOldLen )
return 0;
static const char ch = 0x00;
std::string::size_type nNewLen= strlen(newpart);
const char* szRealNew= newpart == 0 ? &ch : newpart;
while ( (nIdx=str.find(oldpart, nIdx)) != std::string::npos )
{
str.replace(str.begin()+nIdx, str.begin()+nIdx+nOldLen, szRealNew);
nReplaced++;
nIdx += nNewLen;
}
return nReplaced;
}
功能: 从str中查找oldpart, 并替换成newpart.