c# - 如何从 c# 中的 img html 标签获取图像源属性值?
问题描述
我的字符串变量存储一组 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);
推荐阅读
- c# - 如何从列表中为对象分配值?统一 3D C#
- node.js - 生成的路由无法访问 crbug/1173575,非 JS 模块文件已弃用 AdonisJS
- javascript - 获取 Google 跟踪代码管理器简单表字段值并正确格式化多个值以添加到 API 调用的末尾
- php - PHP Laravel - 如何将 Spatie Async 用于并发函数
- bash - 如何在bash中将动态脚本更改为静态脚本?
- c++ - 按下输入键且没有用户输入时捕获异常
- php - Laravel 8 - 在现有布局中包含刀片布局
- javascript - 未捕获的错误:引发了跨域错误。React 无法访问 React Js 开发中的实际错误对象
- php - 如果购物车中的产品类别需要在结帐时注册 Woocommerce
- symbolic-math - 用数字代替参与微分的变量