[英]c++ - Replace string with double quotes to a string with double quotes
我正在編寫此軟件,這使我在網站上的工作更加輕松。 我只是輸入了一個腳本,它將用跨度為我突出顯示代碼。 到了那里,但是我有一個問題。 (我目前正在突出顯示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;
}
}
當我運行它時,它將提示:/dataValues.dat:<span class ='yellow_code'>“ / dataValues.dat” </ span>無限。
怎么了?
問候,丹妮
問題在這里: pos = end_pos + 1;
這可以讓您的索引比字符串短。
讓我們看看您的示例發生了什么:
original
是bla..."/dataValues.dat"...
和end_pos
是pos + 16
bla...<span class='yellow_code'>"/dataValues.dat"</span>...
end_pos
並未更改,並指向黃色的第一個l
! 您必須在end_pos
添加<span...>
和</span>
的長度,以使其指向</span>
您沒有添加添加到字符串中的開始和結束標簽的大小。 這是一個如何做的例子
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
}
}
更新
在查看pos
, found
和start_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
}
在線嘗試
您應該std::regex
text = std::regex_replace(text,std::regex("string to be replaced"),"string with it earlier should be replaced");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.