-1

我正在尝试使用 c++ 来编辑大量的 html 文件。除了我尝试编辑 src 标签外,所有这些都在工作。我认为这是因为引号。这是我的代码。

string strReplace2 = "src=\"\""; //string to replace

strTemp = "src=\"http://localhost/Media/TV Shows/The Big Bang Theory Season 6/" + filename + "\"";

当我运行程序时,除了写入文件的那部分之外,一切正常。

4

1 回答 1

0

你可以试试这个来替换引号之间的东西..

void replace_between_quotes(std::string &str, const std::string &replacement)
{
    std::size_t pos_f = str.find_first_of("\"");
    std::size_t pos_e = str.find_last_of("\"");

    if (pos_f < pos_e && pos_f != std::string::npos && pos_e != std::string::npos)
    {
        std::size_t len = pos_e - pos_f - 1;
        str.replace(pos_f + 1, len, replacement);
    }
}

int main()
{
    std::string filename = "somefile";
    std::string str = "src=\"http://localhost/Media/TV Shows/The Big Bang Theory Season 6/" + filename + "\"";
    replace_between_quotes(str, filename);
    std::cout<<str;
}
于 2013-12-24T01:14:50.917 回答