Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007176
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:36:06+00:00 2026-05-20T08:36:06+00:00

I have simple method in C# : public static string BetweenOf(string ActualStr, string StrFirst,

  • 0

I have simple method in C# :

public static string BetweenOf(string ActualStr, string StrFirst, string StrLast)
        {
            return ActualStr.Substring(ActualStr.IndexOf(StrFirst) + StrFirst.Length, (ActualStr.Substring(ActualStr.IndexOf(StrFirst))).IndexOf(StrLast) + StrLast.Length);
        }

How can i optimise this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:36:06+00:00Added an answer on May 20, 2026 at 8:36 am

    Here’s how the code from @Chris here stacks up against a regular expression test:

    void Main()
    {
        string input = "abcdefghijklmnopq";
        string first = "de";
        string last = "op";
        Regex re1 = new Regex("de(.*)op", RegexOptions.None);
        Regex re2 = new Regex("de(.*)op", RegexOptions.Compiled);
    
        // pass 1 is JIT preheat
        for (int pass = 1; pass <= 2; pass++)
        {
            int iterations = 1000000;
            if (pass == 1)
                iterations = 1;
    
            Stopwatch sw = Stopwatch.StartNew();
            for (int index = 0; index < iterations; index++)
                BetweenOfFixed(input, first, last);
            sw.Stop();
            if (pass == 2)
                Debug.WriteLine("IndexOf: " + 
                    sw.ElapsedMilliseconds + "ms");
    
            sw = Stopwatch.StartNew();
            for (int index = 0; index < iterations; index++)
                BetweenOfRegexAdhoc(input, first, last);
            sw.Stop();
            if (pass == 2)
                Debug.WriteLine("Regex adhoc: " + 
                    sw.ElapsedMilliseconds + "ms");
    
            sw = Stopwatch.StartNew();
            for (int index = 0; index < iterations; index++)
                BetweenOfRegexCached(input, first, last);
            sw.Stop();
            if (pass == 2)
                Debug.WriteLine("Regex uncompiled: " +
                    sw.ElapsedMilliseconds + "ms");
    
            sw = Stopwatch.StartNew();
            for (int index = 0; index < iterations; index++)
                BetweenOfRegexCompiled(input, first, last);
            sw.Stop();
            if (pass == 2)
                Debug.WriteLine("Regex compiled: " +
                    sw.ElapsedMilliseconds + "ms");
        }
    }
    
    public static string BetweenOfFixed(string ActualStr, string StrFirst,
        string StrLast)
    {
        int startIndex = ActualStr.IndexOf(StrFirst) + StrFirst.Length;
        int endIndex = ActualStr.IndexOf(StrLast, startIndex);
        return ActualStr.Substring(startIndex, endIndex - startIndex);
    }
    
    public static string BetweenOfRegexAdhoc(string ActualStr, string StrFirst,
        string StrLast)
    {
        // I'm assuming you don't replace the delimiters on every call
        Regex re = new Regex("de(.*)op", RegexOptions.None);
        return re.Match(ActualStr).Groups[1].Value;
    }
    
    private static Regex _BetweenOfRegexCached =
        new Regex("de(.*)op", RegexOptions.None);
    public static string BetweenOfRegexCached(string ActualStr, string StrFirst,
        string StrLast)
    {
        return _BetweenOfRegexCached.Match(ActualStr).Groups[1].Value;
    }
    
    private static Regex _BetweenOfRegexCompiled =
        new Regex("de(.*)op", RegexOptions.Compiled);
    public static string BetweenOfRegexCompiled(string ActualStr, string StrFirst,
        string StrLast)
    {
        return _BetweenOfRegexCompiled.Match(ActualStr).Groups[1].Value;
    }
    

    Output:

    IndexOf: 1419ms
    Regex adhoc: 7788ms
    Regex uncompiled: 1074ms
    Regex compiled: 682ms
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple extension method on the int type so I can do
I have a simple setter method for a property and null is not appropriate
I have some very simple code to generate an assembly and invoke a method
I am posting a very simple form using a method I have used frequently
This is, probably, a very simple answer for someone. I have a method with
I have a simple method to compare an array of FileInfo objects against a
I have a quick and straighforward question: I have this simple class: public class
I have a really simple class with two methods; One that will be called
My problem is the following. I have a method which simply takes an XML
I have simple regex \.*\ for me its says select everything between and ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.