2009-09-21 84 views
11

我已經花了近一個半小時試圖找出如何運行一個簡單的搜索,並在C string對象++上取代。字符串替換C++

我有三個字符串對象。

string original, search_val, replace_val; 

我想在original運行搜索命令爲search_valreplace_val全部替換。

NB:答案純C++只。環境是Mac OSX Leopard上的XCode。

回答

33

循環應當與找工作和替換

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

你能解釋一下你的代碼是如何工作的? 謝謝:) – 2009-09-21 01:40:20

+0

添加評論。 – 2009-09-21 10:36:12

+0

這是一個小挑剔,但是你的函數名有一個錯字(「Repalce」),在你的,非常優雅和格式良好的代碼中看起來有點不合適。 – 2009-09-21 11:38:53

4
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(); 
} 
+0

感謝您的回答 – 2009-09-22 16:13:32

1

對於這裏的比較是純C函數: http://www.pixelbeat.org/libs/string_replace.c

+1

短語「字符串插值」是什麼意思?我在您提供的鏈接的標題中找到了它。 – 2009-09-21 13:53:29

+0

如果你查找「插值」,你應該得到一個描述。例如shell變量插值是用「value」替換$ name的過程。 – pixelbeat 2009-09-22 13:43:00

1

一點點優雅:

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); 
} 
0

簡單...

但僅限於保換單個字符!

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

result --> foo = "abcde"; 
1
#include <boost/algorithm/string.hpp> 

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

,或者,如果你想就地更換

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

這可能會導致更快的執行速度,並保留原來的,如果想要的。

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(); 

}

0

這可能是你的字符串的最集中的版本替換:

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

經過測試的代碼示例。

如果你想返回的字符串使用:

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