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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:51:26+00:00 2026-05-29T20:51:26+00:00

I have very large string lists and arrays and i found 2 issues that

  • 0

I have very large string lists and arrays and i found 2 issues that i want to resolve:

  1. Remove all entries that are blank strings
  2. Remove all entries that are just whitespace

These can be 2 different solution . . not sure if there is a faster way compared to a basic loop or this:

array = array.Where(r=>!String.IsNullOrEmpty(r.Trim());
  • 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-29T20:51:29+00:00Added an answer on May 29, 2026 at 8:51 pm

    For a List<T> there’s a potentially-faster equivalent, which performs the removal in-place, RemoveAll:

    // We can do better than this - see below...
    list.RemoveAll(r => String.IsNullOrEmpty(r.Trim()));
    

    This may be faster in terms of how the repositioning within the list is performed.

    It also depends on whether you want in-place removal, of course. Personally I’d normally prefer the LINQ approach as it’s more flexible and more idiomatic these days – I usually treat collections as immutable sequences, even if they’re not really 🙂

    One point to note: you don’t need to trim a string to find out whether or not it’s got any whitespace. You can use string.IsNullOrWhiteSpace, which should really be called IsNullOrEmptyOrWhitespace – basically “does it not have content”.

    This could easily make a very significant difference to performance – if you have a lot of long strings, there’s no point in having an O(N) operation (Trim) just to determine that there’s some content… and there’s no point creating a new string when you’re just going to throw it away again.

    Note: sizes changed from earlier version to give good differentiation between final three cases…

    Here’s an example:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    
    public class Test
    {
        static void Main()
        {
            List<string> list = new List<string>();
            string longString = new string('x', 1000) + " ";
            for (int i = 0; i < 1000000; i++)
            {
                list.Add(i % 100 == 0 ? "" : longString);
            }
    
            Stopwatch sw = Stopwatch.StartNew();
            list.Where(r=> !string.IsNullOrEmpty(r.Trim())).ToList();
            sw.Stop();
            Console.WriteLine("IsNullOrEmpty(Trim): {0}", sw.ElapsedMilliseconds);
    
            GC.Collect();
    
            sw = Stopwatch.StartNew();
            list.Where(r=> !string.IsNullOrWhiteSpace(r)).ToList();
            sw.Stop();
            Console.WriteLine("IsNullOrWhitespace: {0}", sw.ElapsedMilliseconds);
    
            GC.Collect();
    
            sw = Stopwatch.StartNew();
            List<string> listResult = new List<string>();
            int countList = list.Count;
            for (int i = 0; i < countList; i++)
            {
                string item = list[i];
                if (!string.IsNullOrWhiteSpace(item))
                {
                    listResult.Add(item);
                }
            }
            sw.Stop();
            Console.WriteLine("New list: {0}", sw.ElapsedMilliseconds);
    
            GC.Collect();
    
            // This has to be last, as it modifies in-place 
            sw = Stopwatch.StartNew();
            list.RemoveAll(r => string.IsNullOrWhiteSpace(r));
            sw.Stop();
            Console.WriteLine("List.RemoveAll: {0}", sw.ElapsedMilliseconds);
        }        
    }
    

    Sample results on my laptop:

    IsNullOrEmpty(Trim): 3573
    IsNullOrWhitespace: 452
    New list: 232
    List.RemoveAll: 153
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that needs to handle very large strings between a SQL
I have a very large code base that contains extensive unit tests (using CppUnit).
I have a very large file that looks like this (see below). I have
I have a very large codebase (read: thousands of modules) that has code shared
I have a very large query that follows the format below: select ... from
I have a very large string (HTML) and in this HTML there is particular
I have a very large response from server of JSON string. I converted it
I have a very large form that represents a month or more of data.
I have a large map of String->Integer and I want to find the highest
I have a very large Form with many date fields that need to be

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.