我的字符串变量存储一组 HTML 标签以及 img 源文件链接。
string str = "<div> <img src="https://i.testimg.com/images/g/test/s-l400.jpg" style="width: 100%;"> <div>Test</div> </div>"
如何从字符串 str 中获取 src 属性值。显然,我必须将 src 的值分配给另一个变量。
如何从c#中的img html标签获取src属性值?
以下代码将提取src
属性的值。
string str = "<div> <img src=\"https://i.testimg.com/images/g/test/s-l400.jpg\" style=\"width: 100%;\"> <div>Test</div> </div>";
// Get the index of where the value of src starts.
int start = str.IndexOf("<img src=\"") + 10;
// Get the substring that starts at start, and goes up to first \".
string src = str.Substring(start, str.IndexOf("\"", start) - start);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句