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 6617855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:45:15+00:00 2026-05-25T20:45:15+00:00

The title explains it all. It seems simple enough, so I must be overlooking

  • 0

The title explains it all. It seems simple enough, so I must be overlooking something stupid. Here’s what I’ve got.

    private string getBetween(string strSource, string strStart, string strEnd)
    {
        int start, end;
        if (strSource.Contains(strStart) && strSource.Contains(strEnd))
        {
            start = strSource.IndexOf(strStart, 0) + strStart.Length;
            end = strSource.IndexOf(strEnd, start);
            return strSource.Substring(start, end - start);
        }
        else
        {
            return "";
        }
    }

Thanks, guys.

  • 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-25T20:45:15+00:00Added an answer on May 25, 2026 at 8:45 pm

    As couple of peoples said the problem that you code is working on very specific input, it’s all because of this start and end IndexOf magic =) But when you try to update you code to work correct on more inputs you will get into problem that your code become very long with many indexes, comparsions, substrings, conditions and so on. To avoid this i like to recommend you use regular expressions with theirs help you can express what you need on special language.

    Here is the sample which solves your problem with regular expressions:

            public static string getBetween(string source, string before, string after)
            {
                var regExp = new Regex(string.Format("{0}(?<needle>[^{0}{1}]+){1}",before,after));
                var matches = regExp.Matches(source).Cast<Match>(). //here we use LINQ to
                    OrderBy(m => m.Groups["needle"].Value.Length).  //find shortest string
                    Select(m => m.Groups["needle"].Value);          //you can use foreach loop instead
                return matches.FirstOrDefault();
            }
    

    All tricky part is {0}(?<needle>[^{0}{1}]+){1} where 0 – before string and 1 – after string. This expression means that we nned to find string that lies beetween 0 and 1, and also don’t contains 0 and 1.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title explains all... I have this snippet of code in my application: String
Title pretty much explains it all, its the last thing I'm trying to work
The title pretty much explains it all. I'm creating a mac app and I
Not sure the title explains what I want to do, so here goes: I
I think the title explains it all but I am going deeper into my
Basically the title explains it all. When the page loads, it first loads the
The title explains it all. What's the difference? var $foo; $bar; What's the main
I need to rename all the images as the title explains in all sub-folders.
Title pretty much explains it all. I need to get some hardware information such
The title pretty much explains it all, I have a Member object that references

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.