: あなたは
に
public static string StripHtmlTags(string source)
{
if (string.IsNullOrEmpty(source))
{
return string.Empty;
}
var array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = let;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}
をHTMLタグを取り除くためにそれを使用して、文字列を数えることができるあなたのカウントは次のようになります。そして、それは簡単です:
string html = "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?";
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
int length = doc.DocumentNode.InnerText.Length; // 114
int imageCount = doc.DocumentNode.Descendants("img").Count(); // 3
これはあなたのサンプル中DocumentNode.InnerText
戻ると、あなたには、いくつかのスペースをスキップしてきたものである:
There is some nice images in this string. I would like to ask you how Can I can I get the Lenght of the string?
:
はこれを使用してください。 それに基づいて解決策が必要な場合は教えてください –
この回答を見て、HTMLタグを削除してください:http://stackoverflow.com/a/18154046/5119765その後、文字列の長さを取得できます。 –
あなたの最適なオプションは、[Html Agility Pack](https://htmlagilitypack.codeplex.com/)のようなhtmlパーサーを使用して、コンテンツの文字の長さとイメージタグの数を正しくカウントできるようにすることです。 – juharr