Строковая замена в C++

Какова периодическая "ручная работа", которой требует система?

Вы знаете, те небольшие задания, которые неожиданно возникают время от времени, которые еще не были автоматизированы. Как Вы фиксируете его и как Вы распознаете его.

11
задан smorgan 22 September 2009 в 14:58
поделиться

3 ответа

Цикл должен работать с поиском и заменой

void searchAndReplace(std::string& value, std::string const& search,std::string const& replace)
{
    std::string::size_type  next;

    for(next = value.find(search);        // Try and find the first match
        next != std::string::npos;        // next is npos if nothing was found
        next = value.find(search,next)    // search for the next match starting after
                                          // the last match that was found.
       )
    {
        // Inside the loop. So we found a match.
        value.replace(next,search.length(),replace);   // Do the replacement.
        next += replace.length();                      // Move to just after the replace
                                                       // This is the point were we start
                                                       // the next search from. 
    }
}
32
ответ дан 3 December 2019 в 01:33
поделиться
size_t start = 0;
while(1) {
  size_t where = original.find(search_val, start);
  if(where==npos) {
    break;
  }
  original.replace(where, search_val.size(), replace_val);
  start = where + replace_val.size();
}
4
ответ дан 3 December 2019 в 01:33
поделиться

Для сравнения функция на чистом C: http://www.pixelbeat.org/libs/string_replace.c

1
ответ дан 3 December 2019 в 01:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: