如何查找和replacestring?
如果s是一个std::string ,那么是否有如下的函数? 
 s.replace("text to replace", "new text"); 
	
 试试std::string::find和std::string::replace 。 
这得到的位置:
 size_t f = s.find("text to replace"); 
这代替了文字:
 s.replace(f, std::string("text to replace").length(), "new text"); 
现在,您可以简单地创build一个函数,以方便您:
 std::string myreplace(std::string &s, const std::string &toReplace, const std::string &replaceWith) { return(s.replace(s.find(toReplace), toReplace.length(), replaceWith)); } 
 是的: replace_all是提升stringalgorithm之一: 
尽pipe它不是一个标准库,它在标准库上有一些东西:
-  基于范围而不是迭代器对的更自然的符号 。 这是很好的,因为你可以嵌套string操作(例如,嵌套在trim内的replace_all)。 这对于标准的库函数来说有点多了。
- 完整性。 这并不难于“更好”; 标准库相当简陋。 例如,提升stringalgorithm可以明确地控制string操作的执行方式(即就地或通过复制)。
我们是否真的需要一个看似如此简单任务的Boost库?
要replace所有出现的子string,使用这个函数:
 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; } 
如果你需要性能,这是一个优化的函数,它修改inputstring,它不会创buildstring的副本:
 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(); } } 
testing:
 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 modified: " << 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 
 #include <iostream> #include <string> using namespace std; int main () { string str("one three two four"); string str2("three"); str.replace(str.find(str2),str2.length(),"five"); cout << str << endl; return 0; } 
产量
 one five two four 
像一些说boost :: replace_all
这里是个虚构的例子:
  #include <boost/algorithm/string/replace.hpp> std::string path("file.gz"); boost::replace_all(path, ".gz", ".zip"); 
 不完全是这样,但std::string有许多replace重载函数。 
通过这个链接来查看每个解释,以及如何使用它们的例子。
 此外,还有几个版本的string::find函数(下面列出),您可以结合使用string::replace 。 
- 找
- RFIND
- find_first_of
- find_last_of
- find_first_not_of
- find_last_not_of
 另外请注意,有几个版本的replace函数可以从<algorithm>中使用,您也可以使用replace函数(而不是string::replace ): 
- 更换
- replace_if
- replace_copy
- replace_copy_if
 // replaced text will be in buffer. void Replace(char* buffer, const char* source, const char* oldStr, const char* newStr) { if(buffer==NULL || source == NULL || oldStr == NULL || newStr == NULL) return; int slen = strlen(source); int olen = strlen(oldStr); int nlen = strlen(newStr); if(olen>slen) return; int ix=0; for(int i=0;i<slen;i++) { if(oldStr[0] == source[i]) { bool found = true; for(int j=1;j<olen;j++) { if(source[i+j]!=oldStr[j]) { found = false; break; } } if(found) { for(int j=0;j<nlen;j++) buffer[ix++] = newStr[j]; i+=(olen-1); } else { buffer[ix++] = source[i]; } } else { buffer[ix++] = source[i]; } } } 
这里是我写的版本,replace给定string中的目标string的所有实例。 适用于任何stringtypes。
 template <typename T, typename U> T &replace ( T &str, const U &from, const U &to) { size_t pos; size_t offset = 0; const size_t increment = to.size(); while ((pos = str.find(from, offset)) != T::npos) { str.replace(pos, from.size(), to); offset = pos + increment; } return str; } 
例:
 auto foo = "this is a test"s; replace(foo, "is"s, "wis"s); cout << foo; 
输出:
 thwis wis a test 
请注意即使searchstring出现在replacestring中,这可以正常工作。