C++: 替换字符串里面的字符

#include <string> 
#include <xstring>

#include <iostream> 


wstring& replace(wstring& str,const   wchar_t*   old_value,const   wchar_t*   new_value) 

int n1 = wcslen(old_value);
int n2 = wcslen(new_value);
for(string::size_type   pos(0);   pos!=string::npos;   pos+=n2)   

    if( (pos=str.find(old_value,pos))!= string::npos  ) 
    str.replace(pos,n1,new_value); 

else  
  break; 

return   str; 


int _tmain(int argc, _TCHAR* argv[])
{

wstring ss = _T("fs我a我d我");
wstring s2 = replace(ss, _T("我"), _T("你"));//s2 = "fs你a你d你"

return 0;
}


你可能感兴趣的:(C++: 替换字符串里面的字符)