.net工具类——获取html字符串中的图片路径列表
调用方法:
HashSet<string> listImg = new HashSet<string>(); GetListHtmlString(TemplateContent, "<img src=\"", listImg); GetListHtmlString(TemplateContent, "<img title=\"image.png\" alt=\"image.png\" src=\"", listImg);
帮助方法:
/// <summary> /// 获取内容的html /// </summary> /// <param name="content"></param> /// <param name="searchStr"></param> /// <returns></returns> public static void GetListHtmlString(string content, string searchStr, HashSet<string> listImg) { if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(searchStr)) return; int thisIndex = 0; while (true) { int startIndex = content.IndexOf(searchStr, thisIndex); if (startIndex == -1) break; startIndex += searchStr.Length; int endIndex = content.IndexOf("\"", startIndex); if (endIndex == -1) break; var listChar = content.Skip(startIndex).Take(endIndex - startIndex).ToArray(); string str = new string(listChar); if (str.Contains("data:image/png;base64")) continue; if (str.Contains("http://")) { str = str.Replace("http://www.aaa.com/Content", "/Content"); } listImg.Add(str); thisIndex = endIndex; } return; }