Я новичок в C ++, я знаю, что мой пост можно найти дубликатом с другими постами, но я хочу сделать так, чтобы заменить подстроку в строке, но не все из них.
Этомоя функция поиска и замены подстроки, она работает как другая функция замены:
void findAndReplaceAll(std::string& data, std::string toSearch, std::string replaceStr)
{
//Get the first occurrence
size_t pos = data.find(toSearch);
//Repeat till end is reached
while (pos != std::string::npos)
{
//Replace this occurrence of Sub String
data.replace(pos, toSearch.size(), replaceStr);
//Get the next occurrence from the current position
pos = data.find(toSearch, pos + replaceStr.size());
}
}
Моя основная функция:
int main()
{
std::string format = "h 'o''cloch' a, zzzz";
findAndReplaceAll(format, "h", "%h");
return 0;
}
Вывод, который я хочу, это просто заменить первую 'h 'но не второй' h 'один.
"%h 'o''cloch' a,zzzz";