Lub*_*rek 5 html c# string image
我有包含 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?";
Run Code Online (Sandbox Code Playgroud)
我想获取没有图像的字符串长度和图像数量。所以,结果应该是:
int strLenght = 111;
int imagesCount= 3;
Run Code Online (Sandbox Code Playgroud)
请问您能告诉我最有效的方法吗?
谢谢
如果您想在正则表达式的帮助下完成此操作,正如我在上面的评论中提到的那样。请使用以下代码
var regex = new System.Text.RegularExpressions.Regex("<img[^>]*/>");
var plainString = regex.Replace(str, "");
// plainString.length will be string length without images
var cnt = regex.Matches(str).Count; // cnt will be number of images
Run Code Online (Sandbox Code Playgroud)