69,368
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
int main()
{
using namespace std;
string str = "Hello world! Hello world! Hello world! Hello world! ";
string s1 = "Hello";
string s2 = "你好世界!";
size_t pos = str.find(s1);
while (pos != string::npos) {
str.replace(pos, s1.length(), s2);
pos = str.find(s1);
}
cout << str << endl;
return 0;
}
static inline std::string ReplaceString(std::string strSrc,std::string strPos,std::string strStr)
{
std::basic_string<char>::iterator IterL;
std::basic_string <char>::size_type indexCh1a = 0;
std::basic_string <char>::size_type off = 0;
std::string result6a = strSrc;
if (strPos.empty())
{
return strSrc;
}
while((indexCh1a = strSrc.find(strPos,off)) != std::string::npos)
{
IterL = strSrc.begin( ) + indexCh1a;
off = indexCh1a + strStr.size();
result6a = strSrc.replace( IterL , IterL+strPos.size() , strStr );
}
return result6a;
}
void f(char *Src, char *DisPlace)
{
int len = strlen(DisPlace) + 1;
char * tmp = (char*)malloc(len);
memset(tmp, 0, strlen(DisPlace) + 1 );
for (int i = 0; i < ( strlen(Src) + 1); ++ i)
{
memcpy(tmp, Src, len);
if( 0 == strcmp(tmp, DisPlace ) )
{
memcpy(Src, tmp, len);
i += len;
}
}
}