C# 如何在C上获得字符串的中心部分
我需要Rocky44的中心线,只使用CC# 如何在C上获得字符串的中心部分,c#,.net,C#,.net,我需要Rocky44的中心线,只使用C Hi <a href="http://example.com/index.php?action=profile"><span>Rocky44</span></a> 使用html解析器。我将使用 使用html解析器。我将使用 你在正确的轨道上;您只是没有正确地转义您的引用: string[] result = temp.Split(new string[] { "<a href=\"http://exa
Hi <a href="http://example.com/index.php?action=profile"><span>Rocky44</span></a>
使用html解析器。我将使用
使用html解析器。我将使用
你在正确的轨道上;您只是没有正确地转义您的引用:
string[] result = temp.Split(new string[] { "<a href=\"http://example.com/index.php?action=profile\"><span>" , "</span></a>" }, StringSplitOptions.RemoveEmptyEntries);
当然,这是假设您的输入总是以给定的格式进行的。正如I4V提到的,如果你想做更复杂的事情,HTML解析器可能会派上用场;您只是没有正确地转义您的引用:
string[] result = temp.Split(new string[] { "<a href=\"http://example.com/index.php?action=profile\"><span>" , "</span></a>" }, StringSplitOptions.RemoveEmptyEntries);
当然,这是假设您的输入总是以给定的格式进行的。正如I4V所提到的,如果您想做更复杂的事情,HTML解析器可能会派上用场。找到并使用该方法的索引
然后调整文本的长度,使用该方法获得所需的文本
string FindLinkText(string linkHtml)
{
int startIndex = linkHtml.IndexOf("<span>") + "<span>".Length,
length = linkHtml.IndexOf("</span>") - startIndex;
return linkHtml.Substring(startIndex, length);
}
使用该方法查找和的索引
然后调整文本的长度,使用该方法获得所需的文本
string FindLinkText(string linkHtml)
{
int startIndex = linkHtml.IndexOf("<span>") + "<span>".Length,
length = linkHtml.IndexOf("</span>") - startIndex;
return linkHtml.Substring(startIndex, length);
}
如果你只想得到这类东西,比如这类HTML,那么我会使用正则表达式。否则,不要使用它
如果你只想得到这类东西,比如这类HTML,那么我会使用正则表达式。否则,不要使用它
@法达蓬克:他已经展示了他迄今为止所做的。@法达蓬克:他已经展示了他迄今为止所做的。@user2430116:我很高兴。请接受回答,以便人们知道您的问题已经解决。@user2430116:我很高兴。请接受回答,以便人们知道您的问题已解决。
string[] result = temp.Split(new string[] { "<a href=\"http://example.com/index.php?action=profile\"><span>" , "</span></a>" }, StringSplitOptions.RemoveEmptyEntries);
string FindLinkText(string linkHtml)
{
int startIndex = linkHtml.IndexOf("<span>") + "<span>".Length,
length = linkHtml.IndexOf("</span>") - startIndex;
return linkHtml.Substring(startIndex, length);
}
string HTML = @"Hi <a href="http://example.com/index.php?action=profile"><span>Rocky44</span></a>"
var result = Regex.Match(HTML, @".*<a.*><span.*>(.*)</span></a>").Groups[1].Value;