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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:50:24+00:00 2026-05-11T06:50:24+00:00

I find that my program is searching through lots of lengthy strings (20,000+) trying

  • 0

I find that my program is searching through lots of lengthy strings (20,000+) trying to find a particular unique phrase.

What is the most efficent method for doing this in C#?

Below is the current code which works like this:

  1. The search begins at startPos because the target area is somewhat removed from the start
  2. It loops through the string, at each step it checks if the substring from that point starts with the startMatchString, which is an indicator that the start of the target string has been found. (The length of the target string varys).
  3. From here it creates a new substring (chopping off the 11 characters that mark the start of the target string) and searches for the endMatchString

I already know that this is a horribly complex and possibly very inefficent algorithm. What is a better way to accomplish the same result?

string result = string.Empty;     for (int i = startPos; i <= response.Length - 1; i++) {    if (response.Substring(i).StartsWith(startMatchString))    {        string result = response.Substring(i).Substring(11);        for (int j = 0; j <= result.Length - 1; j++)        {            if (result.Substring(j).StartsWith(endMatchString))            {                return result.Remove(j)            }        }    } } return result; 
  • 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. 2026-05-11T06:50:25+00:00Added an answer on May 11, 2026 at 6:50 am

    You can use String.IndexOf, but make sure you use StringComparison.Ordinal or it may be one order of magnitude slower.

    private string Search2(int startPos, string startMatchString, string endMatchString, string response) {     int startMarch = response.IndexOf(startMatchString, startPos, StringComparison.Ordinal);     if (startMarch != -1) {         startMarch += startMatchString.Length;         int endMatch = response.IndexOf(endMatchString, startMarch, StringComparison.Ordinal);         if (endMatch != -1) { return response.Substring(startMarch, endMatch - startMarch); }     }     return string.Empty; } 

    Searching 1000 times a string at about the 40% of a 183 KB file took about 270 milliseconds. Without StringComparison.Ordinal it took about 2000 milliseconds.
    Searching 1 time with your method took over 60 seconds as it creates a new string (O(n)) each iteration, making your method O(n^2).

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

Sidebar

Related Questions

I have a program that is searching a maze to find the best way
I'm trying to write a program that would find the minimum spanning tree. But
I am trying to make a program that will find similar images from a
I am trying to write a prolog program that can find the tiles that
While trying to profile my Perl program, I find that Math::Complex is taking up
I am searching through a tree to find a value that is passed. Unfortunately,
I'm looking to find information on how to update my XCode program that has
The MSDN docs state that I can find the WCF Test Client in: C:\Program
I'm writing a python program that deals with a fair amount of strings/files. My
I've been searching through the entire manuals and I can't find a single mention

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.