我有一段文件使用byte读取到内存中了文件的前半部分是html后半部分是图片我该如何使用C#来剔除全半部分的html
可以使用字符串的IndexOf方法来找到图片的起始位置,然后使用Substring方法截取出图片部分,最后将剩余的部分作为纯html处理。具体代码如下:
byte[] fileBytes = File.ReadAllBytes(filePath);
string html = Encoding.UTF8.GetString(fileBytes);
int imgStartIndex = html.IndexOf("<img");
if (imgStartIndex > 0)
{
string pureHtml = html.Substring(0, imgStartIndex);
// 处理纯html部分
string imgPart = html.Substring(imgStartIndex);
// 处理图片部分
}
else
{
// 没有图片,处理全部html部分
}
原文地址: https://www.cveoy.top/t/topic/JTi 著作权归作者所有。请勿转载和采集!