我如何查找和替換(匹配整個單詞)。 我有這個。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