字符串替换C++
我已经花了近一个半小时试图找出如何运行一个简单的搜索,并在C string
对象++上取代。字符串替换C++
我有三个字符串对象。
string original, search_val, replace_val;
我想在original
运行搜索命令为search_val
与replace_val
全部替换。
NB:答案纯C++只。环境是Mac OSX Leopard上的XCode。
循环应当与找工作和替换
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.
}
}
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();
}
感谢您的回答 – 2009-09-22 16:13:32
对于这里的比较是纯C函数: http://www.pixelbeat.org/libs/string_replace.c
短语“字符串插值”是什么意思?我在您提供的链接的标题中找到了它。 – 2009-09-21 13:53:29
如果你查找“插值”,你应该得到一个描述。例如shell变量插值是用“value”替换$ name的过程。 – pixelbeat 2009-09-22 13:43:00
一点点优雅:
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);
}
简单...
但仅限于保换单个字符!
#include <algorithm>
string foo = "abc.e";
std::replace(foo.begin(), foo.end(),'.','d');
result --> foo = "abcde";
#include <boost/algorithm/string.hpp>
string newstring = boost::replace_all_copy(original, search_val, replace_val);
,或者,如果你想就地更换
boost::replace_all(original, search_val, replace_val);
这可能会导致更快的执行速度,并保留原来的,如果想要的。
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();
}
这可能是你的字符串的最集中的版本替换:
for (string::size_type index = 0 ;
(index = value.find(from, index)) != string::npos ;
index += to.size())
value.replace(index, from.size(), to);
经过测试的代码示例。
如果你想返回的字符串使用:
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
你能解释一下你的代码是如何工作的? 谢谢:) – 2009-09-21 01:40:20
添加评论。 – 2009-09-21 10:36:12
这是一个小挑剔,但是你的函数名有一个错字(“Repalce”),在你的,非常优雅和格式良好的代码中看起来有点不合适。 – 2009-09-21 11:38:53