c ++ - 将带双引号的字符串替换为带双引号的字符串

时间:2017-03-28 07:49:51

标签: c++ string c++11 replace

我正在编写这个软件,使我在我的网站上的工作变得更容易一些。我只是放入一个脚本,它将突出显示我的代码。它到了那里,但我有一个问题。 (我目前正致力于突出UnityC#)

我希望字符串为黄色,包括双引号,但我现在尝试它的方式会导致无限循环。我知道我在做引号错误,但我无法弄清楚那是什么。请帮帮我:)。

变量原件由std::string original ((istreambuf_iterator<char>(currentFile)), istreambuf_iterator<char>());创建,其中currentFile是我加载的脚本

std::size_t pos = 0;
while(original.find("\"", pos) != std::string::npos)
{
    //find the first occurrence of the quote
    std::size_t found = original.find("\"", pos);
    if(found != std::string::npos)
    {
        //save the start position of the string
        std::size_t start_pos = found;
        //save the end position by searching for the next quotation mark
        std::size_t end_pos = original.find_first_of("\"", start_pos+1);
        //Calculate the size of the string
        std::size_t length = end_pos-start_pos;
        //Make a copy of the word without the quotation marks
        std::string originalWord = original.substr(start_pos+1, length-1);
        //Make the new word with span, original word(without the quotation marks) and add quotation marks around the word
        std::string newWord = std::string("<span class='yellow_code'>") + "\"" + originalWord + "\"" + "</span>";
        std::cout<<originalWord<<" : "<<newWord<<std::endl;
        //Replace the string WITH the quotation marks for the newWord
        original.replace(start_pos, length+1, newWord);
        //Set the position to after the string
        pos = end_pos+1;
    }
    else
    {
        pos = found+1;
    }
}

当我运行它时,它会cout:/ dataValues.dat:&lt; span class =&#39; yellow_code&#39;&gt;&#34; /dataValues.dat"&lt; / span&gt;无限的。

有什么问题?

此致 达尼

3 个答案:

答案 0 :(得分:3)

问题在于:pos = end_pos + 1;可以让你的索引在字符串之前,它比你的序言短。

让我们看看您的示例会发生什么:

  • 最初originalbla..."/dataValues.dat"...end_pospos + 16
  • 替换后,bla...<span class='yellow_code'>"/dataValues.dat"</span>... end_pos未发生变化,并指向黄色的第一个l
  • 在下一次搜索时,您会再次找到相同的引用字符串...

您必须将end_pos<span...>的长度添加到</span>,以便在</span>之后立即指出

答案 1 :(得分:2)

您没有添加添加到字符串的开始和结束标记的大小。 这是一个如何做到这一点的例子

std::string start_tag = "<span class='yellow_code'>";
std::string end_tag = "\"</span>";

std::size_t pos = 0;
while(original.find("\"", pos) != std::string::npos)
{
    std::size_t found = original.find("\"", pos);
    if(found != std::string::npos)
    {
        std::size_t start_pos = found;
        std::size_t end_pos = original.find_first_of("\"", start_pos+1);
        if (end_pos == std::string::npos) // If the number of quotation characters is odd you would get undefined behavior
            break;
        std::size_t length = end_pos - start_pos;
        std::string originalWord = original.substr(start_pos+1, length-1);
        std::string newWord = start_tag + "\"" + originalWord + end_tag; // CHANGED: Added the start and end tags as std::string
        std::cout<<originalWord<<" : "<<newWord<<std::endl;
        original.replace(start_pos, length+1, newWord);
        pos = end_pos + start_tag.size() + end_tag.size(); // CHANGED: Added the size of the two tags
    }
}

<强>更新

在查看posfoundstart_pos后,似乎其中有两个是多余的。

std::size_t start_pos = 0;
while((start_pos = original.find("\"", start_pos)) != std::string::npos)
{
    std::size_t end_pos = original.find_first_of("\"", start_pos+1);
    if (end_pos == std::string::npos) // If the number of quotation characters is odd you would get undefined behavior
        break;
    std::size_t length = end_pos-start_pos;
    std::string originalWord = original.substr(start_pos+1, length-1);
    std::string newWord = start_tag + "\"" + originalWord + end_tag; // CHANGED: Added the start and end tags as std::string
    std::cout << originalWord << " : " << newWord <<std::endl;
    original.replace(start_pos, length+1, newWord);
    start_pos = end_pos + start_tag.size() + end_tag.size(); // CHANGED: Added the size of the two tags
}

试试online

答案 2 :(得分:0)

您应该std::regex

text = std::regex_replace(text,std::regex("string to be replaced"),"string with it earlier should be replaced");