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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:56:25+00:00 2026-06-16T02:56:25+00:00

I am trying to filter a List of strings based on the number of

  • 0

I am trying to filter a List of strings based on the number of words in each string. I am assuming that you would trim any white-space at the ends of the string, and then count the number of spaces left in the string, so that WordCount = NumberOfSpaces + 1. Is that the most efficient way to do this? I know that for filtering based on character count the following is working fine…just cant figure out how to write it succinctly using C#/LINQ.

if (checkBox_MinMaxChars.Checked)
{
    int minChar = int.Parse(numeric_MinChars.Text);
    int maxChar = int.Parse(numeric_MaxChars.Text);

    myList = myList.Where(x => 
                              x.Length >= minChar && 
                              x.Length <= maxChar).ToList();
}

Any ideas of for counting words?

UPDATE: This Worked like a charm…Thanks Mathew:

int minWords = int.Parse(numeric_MinWords.Text);
int maxWords = int.Parse(numeric_MaxWords.Text);

sortBox1 = sortBox1.Where(x => x.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Count() >= minWords &&
                               x.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Count() <= maxWords).ToList();
  • 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-06-16T02:56:27+00:00Added an answer on June 16, 2026 at 2:56 am

    I would approach it in a more simplified manner since you have indicated that a space can be used reliably as a delimiter like so:

    var str = "     the string to split and count        ";
    var wordCount = str.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Count();
    

    EDIT:

    If optimal perforamnce is necessary and memory usage is a concern you could write your own method and leverage IndexOf() (although there are many avenues for implementation on a problem like this, I just prefer reuse rather than from-scratch code design):

        public int WordCount(string s) {
            const int DONE = -1;
            var wordCount = 0;
            var index = 0;
            var str = s.Trim();
            while (index != DONE) {
                wordCount++;
                index = str.IndexOf(" ", index + 1);
            }
            return wordCount;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to take a search string (could be any number of words)
I am trying to write a function that will filter a list of tuples
Im trying to filter rows based on a column say c1 that contains boolean
I am trying to filter result from JSON string based on status selection. I
What I'm trying to do is dynamically change (via filter) list of strings. In
I am trying to split my list of strings at each separate lines using
I'm trying to dynamically filter (or collect) a list based on type: If I
I'm trying to filter a list of elements via ':not()', and jQuery seems to
I am trying to filter a list so it results in a list with
Ok, I am trying to filter a list/dictionary passed to me and clean it

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.