【发布时间】:2020-06-20 22:19:04
【问题描述】:
我有以下三行代码,html 是一个以字符串形式存储的 html 页面。
int startIndex = html.IndexOf("<title>") + 8; // <title> plus a space equals 8 characters
int endIndex = html.IndexOf("</title>") - 18; // -18 is because of the input, there are 18 extra characters after the username.
result = new Tuple<string, bool>(html.Substring(startIndex, endIndex), false);
输入<title>Username012345678912141618</title> 我希望输出Username。但是,代码找不到</title>。我不确定出了什么问题。有谁知道什么可能导致这种行为?
我用三个不同的网页(都来自同一个网站)对其进行了测试,并检查了其中的内容。
【问题讨论】:
-
-18?为什么要从 endIndex 中减去 -18? -
哦,抱歉,应该将其添加到 OP 中。用户名和 之间总是有 18 个字符的附加文本。像这样:
用户名12345678911131517 -
@JasperMW 编辑后问题仍然显示“With the input
<title>Username</title>...”。那么<title>你实际测试的是什么? -
@dxiv 我无法在线发布网站名称,抱歉。它是
用户名 - 站点名称 -
@JasperMW 只需将
18随机字符放在那里。现在写的方式“使用输入<title>Username</title>我希望输出Username”该行直接与您的其他cmets和您刚刚接受的答案相矛盾。