如何用标准c++库替换字符串的一部分?

QString s("hello $name");  // Example using Qt.
s.replace("$name", "Somename");

当前回答

std::string replace(std::string base, const std::string from, const std::string to) {
    std::string SecureCopy = base;

    for (size_t start_pos = SecureCopy.find(from); start_pos != std::string::npos; start_pos = SecureCopy.find(from,start_pos))
    {
        SecureCopy.replace(start_pos, from.length(), to);
    }

    return SecureCopy;
}

其他回答

string.replace(string.find("%s"), string("%s").size(), "Something");

您可以将其包装在一个函数中,但这种单行解决方案听起来是可以接受的。 问题是这只会改变第一次出现,您可能想要遍历它,但它也允许您使用相同的令牌(%s)在此字符串中插入几个变量。

如果你想快速完成,你可以使用两次扫描的方法。 伪代码:

首先解析。找出有多少匹配字符。 展开字符串的长度。 第二个解析。从字符串的末尾开始,当我们得到一个匹配,我们替换,否则我们只是复制字符从第一个字符串。

我不确定这是否可以优化到一个到位的算法。

和一个c++ 11代码示例,但我只搜索一个字符。

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

void ReplaceString(string& subject, char search, const string& replace)
{   
    size_t initSize = subject.size();
    int count = 0;
    for (auto c : subject) { 
        if (c == search) ++count;
    }

    size_t idx = subject.size()-1 + count * replace.size()-1;
    subject.resize(idx + 1, '\0');

    string reverseReplace{ replace };
    reverse(reverseReplace.begin(), reverseReplace.end());  

    char *end_ptr = &subject[initSize - 1];
    while (end_ptr >= &subject[0])
    {
        if (*end_ptr == search) {
            for (auto c : reverseReplace) {
                subject[idx - 1] = c;
                --idx;              
            }           
        }
        else {
            subject[idx - 1] = *end_ptr;
            --idx;
        }
        --end_ptr;
    }
}

int main()
{
    string s{ "Mr John Smith" };
    ReplaceString(s, ' ', "%20");
    cout << s << "\n";

}

我通常用这个:

std::string& replace(std::string& s, const std::string& from, const std::string& to)
{
    if(!from.empty())
        for(size_t pos = 0; (pos = s.find(from, pos)) != std::string::npos; pos += to.size())
            s.replace(pos, from.size(), to);
    return s;
}

它反复调用std::string::find()来定位搜索字符串的其他出现,直到std::string::find()没有找到任何东西。因为std::string::find()返回匹配的位置,所以我们不存在使迭代器失效的问题。

使用std:: string::替换:

s.replace(s.find("$name"), sizeof("$name") - 1, "Somename");

在c++ 11中,你可以像这样使用std::regex:

#include <regex>
...
std::string string("hello $name");
string = std::regex_replace(string, std::regex("\\$name"), "Somename");

对于转义字符,需要使用双反斜杠。