C# 中取图片标签的宽度

来源:百度知道 编辑:UC知道 时间:2024/07/08 20:22:49
C# 我有<img src="/upload/1.jpg" width="100" height="80">这样一个图片标签的字符串,我想在.cs文件中取出这个标签中的width值和height的值。注:这个标签没有id,是以字符串的形式取出来的。 求教各位了!

可以用正则表达式,匹配度高,不用考虑空格之类的,楼上的方法,局限性太高

string str = "<img src=\"/upload/1.jpg\" width=\"100\" height=\"80\">";
string pattern = "width\\s?=\\s?\"(\\d+)\"\\s+height\\s?=\\s?\"(\\d+)";
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Match m = regex.Match(str);

if (m.Success)
{
string width = m.Groups[1].Value; //宽
string height = m.Groups[2].Value; //高
}

新建控制台程序,代码如下,按需求自行修改:

namespace EX02_01
{
class program
{
static void Main(string[] args)
{
string a = "<img src='/