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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:58:01+00:00 2026-05-13T23:58:01+00:00

The code below is designed to take a string in and remove any of

  • 0

The code below is designed to take a string in and remove any of a set of arbitrary words that are considered non-essential to a search phrase.

I didn’t write the code, but need to incorporate it into something else. It works, and that’s good, but it just feels wrong to me. However, I can’t seem to get my head outside the box that this method has created to think of another approach.

Maybe I’m just making it more complicated than it needs to be, but I feel like this might be cleaner with a different technique, perhaps by using LINQ.

I would welcome any suggestions; including the suggestion that I’m over thinking it and that the existing code is perfectly clear, concise and performant.

So, here’s the code:

private string RemoveNonEssentialWords(string phrase)
{
    //This array is being created manually for demo purposes.  In production code it's passed in from elsewhere.
    string[] nonessentials = {"left", "right", "acute", "chronic", "excessive", "extensive", 
                                    "upper", "lower", "complete", "partial", "subacute", "severe",
                                    "moderate", "total", "small", "large", "minor", "multiple", "early",
                                    "major", "bilateral", "progressive"};
    int index = -1;

    for (int i = 0; i < nonessentials.Length; i++)
    {
        index = phrase.ToLower().IndexOf(nonessentials[i]);
        while (index >= 0)
        {
            phrase = phrase.Remove(index, nonessentials[i].Length);
            phrase = phrase.Trim().Replace("  ", " ");
            index = phrase.IndexOf(nonessentials[i]);
        }
    }

    return phrase;
}

Thanks in advance for your help.

Cheers,

Steve

  • 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-13T23:58:02+00:00Added an answer on May 13, 2026 at 11:58 pm

    This appears to be an algorithm for removing stop words from a search phrase.

    Here’s one thought: If this is in fact being used for a search, do you need the resulting phrase to be a perfect representation of the original (with all original whitespace intact), but with stop words removed, or can it be “close enough” so that the results are still effectively the same?

    One approach would be to tokenize the phrase (using the approach of your choice – could be a regex, I’ll use a simple split) and then reassemble it with the stop words removed. Example:

    public static string RemoveStopWords(string phrase, IEnumerable<string> stop)
    {
        var tokens = Tokenize(phrase);
        var filteredTokens = tokens.Where(s => !stop.Contains(s));
        return string.Join(" ", filteredTokens.ToArray());
    }
    
    public static IEnumerable<string> Tokenize(string phrase)
    {
        return string.Split(phrase, ' ');
        // Or use a regex, such as:
        //    return Regex.Split(phrase, @"\W+");
    }
    

    This won’t give you exactly the same result, but I’ll bet that it’s close enough and it will definitely run a lot more efficiently. Actual search engines use an approach similar to this, since everything is indexed and searched at the word level, not the character level.

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

Sidebar

Ask A Question

Stats

  • Questions 346k
  • Answers 346k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How are you importing? Are you sure one of the… May 14, 2026 at 6:05 am
  • Editorial Team
    Editorial Team added an answer var newList = (from item in oldList select new {… May 14, 2026 at 6:05 am
  • Editorial Team
    Editorial Team added an answer In Lua, to view the members of a object, you… May 14, 2026 at 6:05 am

Related Questions

I'm currently modifying a class that has 9 different constructors. Now overall I believe
I have two classes that I would like to merge into a composite. These
I have some PHP code that is designed to make a spreadsheet with formulas
I have a query which is designed to retireve the name field for all
I'm currently working on a jQuery script that will translate the site's text into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.