【发布时间】:2016-08-24 12:31:49
【问题描述】:
我有带有 HTML 图像的字符串,例如:
string str = "There is some nice <img alt='img1' src='img/img1.png' /> images in this <img alt='img2' src='img/img2.png' /> string. I would like to ask you <img alt='img3' src='img/img3.png' /> how Can I can I get the Lenght of the string?";
我想得到没有图像和图像数量的字符串的长度。所以,结果应该是:
int strLenght = 111;
int imagesCount= 3;
你能告诉我最有效的方法吗?
谢谢
【问题讨论】:
-
你可以在正则表达式的帮助下做到这一点。如果您需要基于它的解决方案,请告诉我
-
看看这个答案来删除 HTML 标签:stackoverflow.com/a/18154046/5119765 然后你就可以得到字符串长度了。
-
您最好的选择是使用像Html Agility Pack 这样的html解析器,这样您就可以正确计算内容的字符长度和图像标签的数量。