#include "stdafx.h"
#include <string>
#include <stdarg.h>
#include <memory>
#include <iostream>
#include <map>
using namespace std;
string StringReplace(const string& str_src, const string& from, const string& to) {
if (str_src.empty()
|| from.empty()
|| to.empty())
{
return str_src;
}
string str_dst;
string str_src_tmp = str_src;
size_t from_size = from.size();
size_t to_size = to.size();
// 循环处理未处理字符串
for (size_t pos = str_src_tmp.find(from); string::npos != pos; pos = str_src_tmp.find(from))
{
str_src_tmp.replace(pos, from_size, to); // 替换from到to
str_dst += str_src_tmp.substr(0, from_size + pos); // 获取已处理字符串
str_src_tmp = str_src_tmp.substr(from_size + pos); // 获取未处理字符串
}
// 拼接剩余字符串
str_dst += str_src_tmp;
return str_dst;
}
int main() {
string src = "abcd";
string from = "a";
string to = "A";
string dst = StringReplace(src, from, to);
string src2 = "aaaaaa";
string from2 = "aa";
string to2 = "bb";
string dst2 = StringReplace(src2, from2, to2);
getchar();
return 0;
}
03-15
3165
09-26
1851
09-26
1099