我如何查找和替换(匹配整个单词)。 我有这个。C++查找和替换整个单词
void ReplaceString(std::string &subject, const std::string& search, const std::string& replace)
{
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}
但它dosnt搜索整个词。 例如,如果我尝试
string test = "i like cake";
ReplaceString(test, "cak", "notcake");
它仍然会取代,但我希望它全字匹配。
真的吗?你不能先搜索?今天发布了三个相关问题。我强烈建议与你的同学谈谈。 –
[用另一个字符串替换字符串的一部分]可能的重复(http://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string) – Hcorg
@Hcorg看着那个,它看起来像是这个作者写了完全相同的功能,或者已经尝试过那个(因为这两个样本在功能上是相同的,只有不同的参数名称)。 – lcs