سؤال

giveacodicetagpre.

الاستعمال: giveacodicetagpre.

أنا أستخدم DotTrace وتستخدم هذه الطريقة 33٪ من وحدة المعالجة المركزية الخاصة بي.كيف يمكنني تحسينه.لأنها تعطل طلبي أو أخرج من الذاكرة.هل هو ذكيا أن هذه الطريقة ثابتة؟

DoTTrace عرض 30٪ استخدام وحدة المعالجة المركزية على هذا: giveacodicetagpre.

أدخل وصف الصورة هنا

تحرير: giveacodicetagpre.

آمل أن يساعد هذا الطريقة هذه الطريقة.حاول العثور على سلسلة بين Strbegin ونتر ...

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top