Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C上获得字符串的中心部分_C#_.net - Fatal编程技术网

C# 如何在C上获得字符串的中心部分

C# 如何在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

我需要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://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;