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

首頁 > 編程 > .NET > 正文

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

2024-07-10 12:38:17
字體:
供稿:網(wǎng)友

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

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

代碼如下:
代碼如下:
/// <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); //圖像標簽

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ā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 施甸县| 定陶县| 凉城县| 余庆县| 隆回县| 南江县| 达拉特旗| 榆林市| 武城县| 普兰店市| 新乡市| 延边| 淳安县| 荃湾区| 四平市| 遂昌县| 桐城市| 根河市| 泾阳县| 贵定县| 宾阳县| 九龙县| 桂平市| 广灵县| 永春县| 宁陕县| 桐梓县| 巧家县| 和政县| 衡东县| 昂仁县| 甘肃省| 兴义市| 宁远县| 珠海市| 江城| 闻喜县| 汝阳县| 喜德县| 平昌县| 屯昌县|