#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++实现字符串中特定字符替换的方法,通过自定义函数实现字符串操作,具体步骤包括导入头文件、定义函数、实例演示等。
7413

被折叠的 条评论
为什么被折叠?



