C# 如何提取字符串中两个术语之间的数据?

C# 如何提取字符串中两个术语之间的数据?,c#,C#,我正在尝试提取href=”之间的链接,并将其设置为字符串。有办法吗? 其中的数据会不断变化,它并不总是在同一个域中。提前感谢。快速简便:在href=“和下一个”之间查找文本(因为“永远不会出现在值中): Int32 startIdx=input.IndexOf(“href=\”); if(startIdx

我正在尝试提取href=”之间的链接,并将其设置为字符串。有办法吗?
其中的数据会不断变化,它并不总是在同一个域中。提前感谢。

快速简便:在
href=“
和下一个
之间查找文本(因为
永远不会出现在值中):

Int32 startIdx=input.IndexOf(“href=\”);
if(startIdx<0)返回null;
Int32 endIdx=input.IndexOf(“\”,startIdx);
if(endIdx<0)返回null;
返回input.Substring(startIdx,endIdx-startIdx);

我相信您可以在堆栈的其他位置找到这个,但我会使用string.split()。您可以设置类似的内容

Int32 startIdx = input.IndexOf( "href=\"" );
if( startIdx < 0 ) return null;
Int32 endIdx = input.IndexOf( "\"", startIdx );
if( endIdx < 0 ) return null;
return input.Substring( startIdx, endIdx - startIdx );
publicstaticvoidmain(){
char[]delimiterChars={''','};
字符串文本=”;
string[]words=text.Split(delimiterCars);
}

假设标记的结构总是相同的,那么只需抓取数组中的第三个字符串即可提取所需的值。您也可以搜索href的索引,然后根据该索引拆分字符串,但我会这样做

非常感谢你!非常有用。
Int32 startIdx = input.IndexOf( "href=\"" );
if( startIdx < 0 ) return null;
Int32 endIdx = input.IndexOf( "\"", startIdx );
if( endIdx < 0 ) return null;
return input.Substring( startIdx, endIdx - startIdx );
public static void Main(){

     char[] delimiterChars = { '"', ' ' };

     string text = "<a href=\"https://genius.com/Run-the-jewels-lie-cheat-steal-lyrics\" class=\" song_link\" title=\"Lie, Cheat, Steal by&nbsp;Run&nbsp;the Jewels\">";

      string[] words = text.Split(delimiterChars);
}