Html 为什么string.find(";<;/a>;)找不到"</a>&引用;?
我的程序除了在Html 为什么string.find(";<;/a>;)找不到"</a>&引用;?,html,c++,string,find,Html,C++,String,Find,我的程序除了在中找不到外,运行得非常好。 它可以找到所有东西,例如,它可以找到,,等,但由于某种原因,它不能找到 #include <iostream> #include <string> using namespace std; int main() { string HTML_text; getline(cin, HTML_text, '\t'); cout << endl << "Printing test!";
中找不到外,运行得非常好。
它可以找到所有东西,例如,它可以找到
,
,
等,但由于某种原因,它不能找到
#include <iostream>
#include <string>
using namespace std;
int main()
{
string HTML_text;
getline(cin, HTML_text, '\t');
cout << endl << "Printing test!";
// replacing hyperlink
string sub_string;
int index_begin = HTML_text.find("<a href=") + 8;
string helper = HTML_text.substr(index_begin, HTML_text.size());
int index_end = helper.find(">");
helper.clear();
sub_string = HTML_text.substr(index_begin, index_end);
//substring is made
index_begin = HTML_text.find(">", index_begin) + 1;
index_end = HTML_text.find("</a>"); //HERE IS THE PROBLEM
helper = HTML_text.substr(index_begin, index_end);
cout << "\n\nPrinting helper!\n";
cout << helper << endl << endl;
HTML_text.erase(index_begin, index_end);
HTML_text.insert(index_begin, sub_string);
cout << endl << "Printing results!";
cout << endl << endl << HTML_text << endl << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串HTML_文本;
getline(cin,HTML_文本,'\t');
cout问题不在您假设的位置:index\u end=HTML\u text.find(“”)
工作正常并在字符串中找到包含
的位置:如果观察值index\u end
,您可以很容易地在调试器中看到。如果找不到
,index\u end
将等于),但它是123,而index\u begin
是114
让我们看一下
擦除方法的签名有两个参数,位置和长度,而代码假定第二个参数是结束位置(例如,也是如此)
这不是一个大问题,可以很容易地解决,因为我们可以简单地通过
length = end_position - start_position;
因此,您的固定代码是:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string HTML_text;
getline(cin, HTML_text, '\t');
cout << endl << "Printing test!";
// replacing hyperlink
string sub_string;
int index_begin = HTML_text.find("<a href=") + 8;
string helper = HTML_text.substr(index_begin);
int index_end = helper.find(">");
helper.clear();
sub_string = HTML_text.substr(index_begin, index_end);
//substring is made
index_begin = HTML_text.find(">", index_begin) + 1;
index_end = HTML_text.find("</a>");
helper = HTML_text.substr(index_begin, index_end - index_begin);
cout << "\n\nPrinting helper!\n";
cout << helper << endl << endl;
HTML_text.erase(index_begin, index_end - index_begin);
HTML_text.insert(index_begin, sub_string);
cout << endl << "Printing results!";
cout << endl << endl << HTML_text << endl << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串HTML_文本;
getline(cin,HTML_文本,'\t');
您可以使用“引号(AltGR+7在Windows上)添加内联代码,这样您就可以编写HTML标记:)@ DINGOGRD,我正在做一些测试,并且仍然存在一些问题,它没有在“”的位置准确地设置整数索引尾,我感觉它设置在“”和“< /a>”中间的某个地方。.Well
和其他都是html标记,因此网站将尝试呈现它(a是锚,i是图标,b是粗体等等)。哦,我指的是stackoverflow,如何在不使用(点)的情况下编写标记。在你的代码中,你应该使用“或‘引号,而不是’。请再次阅读substr
的文档,并特别注意其第二个参数的含义。感谢您花时间为我澄清这些问题。
length = end_position - start_position;
#include <iostream>
#include <string>
using namespace std;
int main()
{
string HTML_text;
getline(cin, HTML_text, '\t');
cout << endl << "Printing test!";
// replacing hyperlink
string sub_string;
int index_begin = HTML_text.find("<a href=") + 8;
string helper = HTML_text.substr(index_begin);
int index_end = helper.find(">");
helper.clear();
sub_string = HTML_text.substr(index_begin, index_end);
//substring is made
index_begin = HTML_text.find(">", index_begin) + 1;
index_end = HTML_text.find("</a>");
helper = HTML_text.substr(index_begin, index_end - index_begin);
cout << "\n\nPrinting helper!\n";
cout << helper << endl << endl;
HTML_text.erase(index_begin, index_end - index_begin);
HTML_text.insert(index_begin, sub_string);
cout << endl << "Printing results!";
cout << endl << endl << HTML_text << endl << endl;
}
Printing test!
Printing helper!
link text
Printing results!
<html>
<head>
text to be deleted
</head>
<body>
Hi there!
<b>some bold text</b>
<i>italic</i>
<a href=www.abc.com>www.abc.com</a>
</body>
</html>