Подтвердить что ты не робот

String Заменить на С++

Я провел последние полтора часа, пытаясь выяснить, как запустить простой поиск и заменить на объект string на С++.

У меня есть три строковых объекта.

string original, search_val, replace_val;

Я хочу запустить команду поиска на original для search_val и заменить все вхождения на replace_val.

NB: Ответы только на чистом С++. Окружающая среда XCode на Mac OSX Leopard.

4b9b3361

Ответ 1

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

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. 
    }
}

Ответ 2

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

Немного элегантнее:

void searchAndReplace(std::string& value, std::string const& search,std::string const& replace) {
    for(std::string::size_type idx = value.find(search);match
        idx != std::string::npos;        
        next = value.find(search, idx + replace.size())   
    )
        value.replace(next, search.size(), replace);
}

Ответ 5

#include <boost/algorithm/string.hpp>

string newstring = boost::replace_all_copy(original, search_val, replace_val);

или, если вы хотите заменить на месте

boost::replace_all(original, search_val, replace_val);

Ответ 6

Простой...

Но ограничивается заменой только одного char!!

#include <algorithm>
string foo = "abc.e";
std::replace(foo.begin(), foo.end(),'.','d');

result --> foo = "abcde";

Ответ 7

Это может привести к более быстрому исполнению и сохранению оригинала при желании.

static std::string strreplace( const std::string &original, const std::string &pattern, const std::string &newtext ) {
std::stringstream ss;           
std::string::size_type last = 0;
std::string::size_type it = original.find( pattern, last );
while( it != original.npos ) {
    if( it-last > 0 ) {
        ss << original.substr( last, it - last );
        ss << newtext;
    }
    last = it + pattern.size( );                
    it = original.find( pattern, last );
}
return ss.str( );

}

Ответ 8

Это, вероятно, ваша наиболее концентрированная версия строки replace:

for ( string::size_type index = 0 ;
                       (index = value.find(from, index)) != string::npos ;
                       index += to.size() )
    value.replace(index, from.size(), to);

Ответ 9

Проверенный код с примерами.

Если вы хотите, чтобы возвращаемая строка использовала это:

std::string 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();
    }
    return subject;
}

Если вам нужна производительность, вот оптимизированная функция, которая изменяет входную строку, она не создает копию строки:

void ReplaceStringInPlace(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();
    }
}

Тесты:

std::string input = "abc abc def";
std::cout << "Input string: " << input << std::endl;

std::cout << "ReplaceString() return value: " 
          << ReplaceString(input, "bc", "!!") << std::endl;
std::cout << "ReplaceString() input string not changed: " 
          << input << std::endl;

ReplaceStringInPlace(input, "bc", "??");
std::cout << "ReplaceStringInPlace() input string modified: " 
          << input << std::endl;

Вывод:

Input string: abc abc def
ReplaceString() return value: a!! a!! def
ReplaceString() input string not modified: abc abc def
ReplaceStringInPlace() input string modified: a?? a?? def