现在的位置: 首页 > 综合 > 正文

字符串中指定字符串的替换

2013年01月13日 ⁄ 综合 ⁄ 共 516字 ⁄ 字号 评论关闭

方法一:

string&   replace_all(string&   str,const   string&   old_value,const   string&   new_value)   
{   
    while(true)   {   
        string::size_type   pos(0);   
        if(   (pos=str.find(old_value))!=string::npos   )   
            str.replace(pos,old_value.length(),new_value);   
        else   break;   
    }   
    return   str;   
} 

方法二:

string&   replace_all(string&   str,const   string&   old_value,const   string&   new_value)   
{   
	while(true)   {   
		string::size_type   pos(0);   
		if(   (pos=str.find(old_value))!=string::npos   )   
		{
			//str.replace(pos,old_value.length(),new_value);   
			str.erase(pos, old_value.length());
			str.insert(pos, new_value);
		}
		else   break;   
	}   
	return   str;   
}  

抱歉!评论已关闭.