c#
  • html
  • string
  • image
  • 2016-04-29 11 views 4 likes 
    4

    ずに長さを取得し、例えば:C#HTML文字列 - >私はHTML画像と文字列を持っている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; 
    

    私に最も効果的な方法を教えてください。

    おかげ

    +0

    はこれを使用してください。 それに基づいて解決策が必要な場合は教えてください –

    +0

    この回答を見て、HTMLタグを削除してください:http://stackoverflow.com/a/18154046/5119765その後、文字列の長さを取得できます。 –

    +1

    あなたの最適なオプションは、[Html Agility Pack](https://htmlagilitypack.codeplex.com/)のようなhtmlパーサーを使用して、コンテンツの文字の長さとイメージタグの数を正しくカウントできるようにすることです。 – juharr

    答えて

    1

    私は上記の私のコメントで述べたように、あなたは、正規表現の助けを借りてそれを行うしたい場合。次のコードを使用してください

    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 
    
    2

    私は同様の問題があり、この方法を作成しました。私は例HtmlAgilityPackのため、実際のHTMLパーサーを使用することをお勧めしたい

    int strLength = StripHtmlTags(str).Count; 
    
    +0

    'string'は' IEnumerable 'を実装しているので、代わりに' foreach(char let in source) 'を使うことができます。 – juharr

    +0

    はい、それは間違いなく最適化することができますありがとう –

    3

    : あなたは

    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? 
    
    2

    はMSHTMLに(COM)参照(マイクロソフトHTMLオブジェクトのlib)を追加します次のことができます。

    var doc = (IHTMLDocument2)new HTMLDocument(); 
    doc.write(str); 
    
    Console.WriteLine("Length: {0}", doc.body.innerText.Length); 
    Console.WriteLine("Images: {0}", doc.images.length); 
    
    0

    私は、しかし、私はMS Wordの結果と一致するように最後にTrim()を追加する必要がありました、ジョン・スミス・ソリューションを言っています。あなたが正規表現の助けを借りてこれを行うことができます

    return new string(array, 0, arrayIndex).Trim(); 
    
    関連する問題