国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net(c#)獲取內(nèi)容第一張圖片地址的函數(shù)

2020-01-18 01:05:24
字體:
供稿:網(wǎng)友

首先找到內(nèi)容里面第一個(gè)<img標(biāo)簽的位置,然后找到從這個(gè)起的第一個(gè)>的位置,得到第一張圖片的完整標(biāo)簽。

然后通過分隔空格得到圖片的各個(gè)屬性和屬性值,提取src的值就是圖片的地址

代碼如下:

復(fù)制代碼 代碼如下:

/// <summary>
/// 獲取文中圖片地址
/// </summary>
/// <param name="content">內(nèi)容</param>
/// <returns>地址字符串</returns>
public static string getImageUrl(string content)
{
int mouse = 0;
int cat = 0;
string imageLabel = "";
string imgSrc = "";
string[] Attributes;

do //得到第一張圖片的連接作為主要圖片
{
cat = content.IndexOf("<IMG", mouse);
mouse = content.IndexOf('>', cat);
imageLabel = content.Substring(cat, mouse - cat); //圖像標(biāo)簽

Attributes = imageLabel.Split(' '); //將圖片屬性分開

foreach (string temp_Attributes in Attributes) //得到圖片地址屬性
if (temp_Attributes.IndexOf("src") >= 0)
{
imgSrc = temp_Attributes.ToString();
break;
}
imgSrc = imgSrc.Substring(imgSrc.IndexOf('"') + 1, imgSrc.LastIndexOf('"') - imgSrc.IndexOf('"') - 1); //叢地址屬性中提取地址

} while (imgSrc == "" && cat > 0);

return (imgSrc);
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 双城市| 通城县| 正阳县| 宣汉县| 如皋市| 紫阳县| 白沙| 石棉县| 塔城市| 沐川县| 板桥市| 印江| 衡水市| 梨树县| 光泽县| 明溪县| 南靖县| 仙桃市| 乐亭县| 叶城县| 方城县| 扎兰屯市| 四会市| 临颍县| 全州县| 彰武县| 临汾市| 白银市| 岗巴县| 都昌县| 皋兰县| 班戈县| 阳原县| 朝阳市| 永寿县| 南召县| 凯里市| 尚志市| 陆河县| 谢通门县| 天祝|