Question

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;
        }

Utilisation:

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

J'utilise DotTrace et cette méthode utilise 33% de mon CPU.Comment puis-je l'optimiser.Becousez-lui mon crash de l'application ou je sors de mémoire.Est-il intelligent que cette méthode est statique?

DotTrace Afficher 30% d'utilisation de CPU à ce sujet:

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

Entrez la description de l'image ici

EDIT:

    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>"

J'espère que cela aide ce que faire cette méthode.Essayez de trouver une chaîne entre Strbegin et Strend ...

Était-ce utile?

La solution

Copying part of the string (your first SubString call) just to keep searching in it is bad for performance. Instead, keep your original input string but use the overload on IndexOf that takes a start index and then adjust your index calculation for extracting the result accordingly.

Also, knowing that these strings are not localized, you might gain some by using an ordinal comparer in IndexOf.

Something along the lines of

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;
}

Autres conseils

For your sample input, it appears you are working with HTML fragments.

I suggest using the HTML Agility Pack to parse the HTML - it exposes the result in an easy to query fashion, using LINQ to XML or XPath type syntax. It is a fast and efficient library.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top