Replace part of a string with another string

前端 未结 15 2360
终归单人心
终归单人心 2020-11-22 02:54

Is it possible in C++ to replace part of a string with another string?

Basically, I would like to do this:

QString string(\"hello $name\");
string.re         


        
15条回答
  •  旧巷少年郎
    2020-11-22 03:36

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

提交回复
热议问题