我只需要使用Rocky44的中心字符串C#
Hi <a href="http://example.com/index.php?action=profile"><span>Rocky44</span></a>我尝试了一些分裂的方法,但无法工作
string[] result = temp.Split(new string[] { "<a href=" + "http://example.com/index.php?action=profile" + "><span>" , "</span></a>" }, StringSplitOptions.RemoveEmptyEntries); 示例:
Hi <a href="http://example.com/index.php?action=profile"><span>Rocky44</span></a>To:
Rocky44发布于 2013-05-30 17:42:04
使用html解析器。我将给出一个使用HtmlAgilityPack的示例
string html = @"Hi <a href=""http://example.com/index.php?action=profile""><span>Rocky44</span></a>";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var text = doc.DocumentNode.SelectSingleNode("//span").InnerText;发布于 2013-05-30 17:44:57
使用<span>方法查找</span>和IndexOf的索引。
然后(根据<span>的长度调整)使用String.Substring方法获得所需的文本。
string FindLinkText(string linkHtml)
{
int startIndex = linkHtml.IndexOf("<span>") + "<span>".Length,
length = linkHtml.IndexOf("</span>") - startIndex;
return linkHtml.Substring(startIndex, length);
}发布于 2013-05-30 17:49:19
如果你只想得到这类东西(比如,,类似于HTML),那么我会使用regex。否则,不使用IT。
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;https://stackoverflow.com/questions/16842611
复制相似问题