我在另一个堆栈问题上发现了这个问题:
//http://stackoverflow.com/questions/3418231/c-replace-part-of-a-string-with-another-string
//
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
size_t end_pos = start_pos + from.length();
str.replace(start_pos, end_pos, to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
和我的方法:
string convert_FANN_array_to_binary(string fann_array)
{
string result = fann_array;
cout << result << "\n";
replaceAll(result, "-1 ", "0");
cout << result << "\n";
replaceAll(result, "1 ", "1");
return result;
}
,对于此输入:
cout << convert_FANN_array_to_binary("1 1 -1 -1 1 1 ");
现在,输出应为“110011”
这是方法的输出:
1 1 -1 -1 1 1 // original
1 1 0 1 // replacing -1's with 0's
11 1 // result, as it was returned from convert_FANN_array_to_binary()
我一直在查看replaceAll代码,而且,我真的不确定它为什么用一个0替换连续的-1,然后在最终结果中不返回任何0(和一些1)。 = \
答案 0 :(得分:53)
完整的代码:
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 changed: abc abc def
ReplaceStringInPlace() input string modified: a?? a?? def
答案 1 :(得分:18)
错误发生在str.replace(start_pos, end_pos, to);
来自http://www.cplusplus.com/reference/string/string/replace/
的std :: string docstring& replace ( size_t pos1, size_t n1, const string& str );
您正在使用结束位置,而该函数需要一个长度。
所以改为:
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // ...
}
注意:未经测试。
答案 2 :(得分:11)
这将出现在我的“只使用Boost库”答案的列表中,但无论如何它仍然存在:
您考虑过Boost.String了吗?它具有比标准库更多的功能,并且在功能重叠的地方,Boost.String在我看来具有更自然的语法。
答案 3 :(得分:7)
C ++ 11现在包含具有正则表达式功能的标题<regex>
。来自docs:
// regex_replace example
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main ()
{
std::string s ("there is a subsequence in the string\n");
std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
// using string/c-string (3) version:
std::cout << std::regex_replace (s,e,"sub-$2");
std::cout << std::endl;
return 0;
}
答案 4 :(得分:6)
我找到了之前答案中给出的替换函数,所有函数都在内部使用就地调用str.replace(),在处理大约2 MB长度的字符串时非常慢。具体来说,我调用了像ReplaceAll(str,“\ r”,“”)这样的东西,并且在我的特定设备上,文本文件包含很多换行符,大约需要27秒。然后,我将其替换为仅在新副本中连接子字符串的函数,并且仅用了大约1秒钟。这是我的ReplaceAll()版本:
void replaceAll(string& str, const string& from, const string& to) {
if(from.empty())
return;
string wsRet;
wsRet.reserve(str.length());
size_t start_pos = 0, pos;
while((pos = str.find(from, start_pos)) != string::npos) {
wsRet += str.substr(start_pos, pos - start_pos);
wsRet += to;
pos += from.length();
start_pos = pos;
}
wsRet += str.substr(start_pos);
str.swap(wsRet); // faster than str = wsRet;
}
格雷格
答案 5 :(得分:1)
试试这个:
#include <string>
string replace_str(string & str, const string & from, const string & to)
{
while(str.find(from) != string::npos)
str.replace(str.find(from), from.length(), to);
return str;
}