您的位置:首页 > 其它

字符串查找和替换接口

2016-02-28 09:35 253 查看
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;

}

功能: 从str中查找oldpart, 并替换成newpart.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: