这里是我找到字符串中的序列,并与另一个替换它的代码:单引号++的问题查找和替换功能
std::string find_and_replace(string &source, string find, string replace)
{
size_t j;
for (; (j = source.find(find)) != string::npos ;)
{
source.replace(j, find.length(), replace);
}
return source;
}
一切正常,当我打电话是这样的:
find_and_replace(test, "foo", "bar")
我的申请要求我用两个单引号替换单引号,而不是双引号。例如,我会打电话:
find_and_replace(test, "'", "''")
但是每当我调用此函数时,函数都会因某种原因而冻结。有谁知道可能是什么原因造成这个问题?
编辑:基于我已经得到了答案,我有固定的代码:
std::string find_and_replace(string &source, string find, string replace)
{
string::size_type pos = 0;
while ((pos = source.find(find, pos)) != string::npos) {
source.replace(pos, find.size(), replace);
pos += replace.size();
}
return source;
}
我希望这可以帮助有同样的问题一些人。
嗯,任何原因downvote?没有正当理由的下降是无用的,因为他们不告诉创作者如何改进他们的问题。 – 2009-07-06 16:04:54