0
public static string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
        {
            string[] result = { "", "" };
            int iIndexOfBegin = strSource.IndexOf(strBegin);

            if (iIndexOfBegin != -1)
            {
                // include the Begin string if desired
                if (includeBegin)
                    iIndexOfBegin -= strBegin.Length;

                strSource = strSource.Substring(iIndexOfBegin + strBegin.Length);
                int iEnd = strSource.IndexOf(strEnd);

                if (iEnd != -1)
                {
                    // include the End string if desired
                    if (includeEnd)
                        iEnd += strEnd.Length;

                    result[0] = strSource.Substring(0, iEnd);

                    // advance beyond this segment
                    if (iEnd + strEnd.Length < strSource.Length)
                        result[1] = strSource.Substring(iEnd + strEnd.Length);
                }
            }

            return result;
        }

利用方法:

string[] result = null;
result = HtmlHelper.GetStringInBetween(bits[0], bits[1], tagValuePair.Value, true, true);

私は dottrace を使用していますが、このメソッドは CPU の 33% を使用しています。どうすれば最適化できますか。そのため、アプリケーションがクラッシュしたり、メモリ不足になったりします。このメソッドが静的であることは賢明ですか?

dottrace は、これで CPU の 30% の使用率を示します。

System.String.IndexOf(String, Int32, Int32, StringComparison)

ここに画像の説明を入力

編集:

    GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)

strBegin = "<td class=\"m92_t_col2\">"
strEnd = "</td>"
strSource = "xxxxxxxx<td class=\"m92_t_col2\">Di. 31.01.12</td>xxxxxxxxxxxxxx
includeBegin = true
includeEnd = true

then i will get result
result[0] = "<td class=\"m92_t_col2\">Di. 31.01.12</td>"

これがこのメソッドの機能に役立つことを願っています。strBegin と strEnd の間の文字列を検索してみてください...

4

2 に答える 2

1

検索を続けるためだけに文字列の一部 (最初の SubString 呼び出し) をコピーすると、パフォーマンスが低下します。代わりに、元の入力文字列を保持しますが、開始インデックスを受け取る IndexOf のオーバーロードを使用し、それに応じて結果を抽出するためにインデックス計算を調整します。

また、これらの文字列がローカライズされていないことを知っているので、IndexOf で序数の比較子を使用することで得られる可能性があります。

の線に沿った何か

public static string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
{
    string[] result = { "", "" };
    int iIndexOfBegin = strSource.IndexOf(strBegin, StringComparison.Ordinal);

    if (iIndexOfBegin != -1)
    {
        int iEnd = strSource.IndexOf(strEnd, iIndexOfBegin, StringComparison.Ordinal);

        if (iEnd != -1)
        {
            result[0] = strSource.Substring(
                iIndexOfBegin + (includeBegin ? 0 : strBegin.Length), 
                iEnd + (includeEnd ? strEnd.Length : 0) - iIndexOfBegin);

            // advance beyond this segment
            if (iEnd + strEnd.Length < strSource.Length)
                result[1] = strSource.Substring(iEnd + strEnd.Length);
        }
    }

    return result;
}
于 2012-01-26T19:52:14.897 に答える
0

サンプル入力では、HTMLフラグメントを使用しているようです。

HTML Agility Packを使用してHTMLを解析することをお勧めします。これにより、LINQ to XMLまたはXPathタイプの構文を使用して、クエリを実行しやすい方法で結果が公開されます。これは高速で効率的なライブラリです。

于 2012-01-26T19:18:21.577 に答える