How to remove an empty string from a list of empty strings in C#?


Firstly, set a list with empty string as elements.

List<string> myList = new List<string>() {
   " ",
   " ",
   " "
};

Now let us remove one empty element using index.

myList.RemoveAt(0);

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> myList = new List<string>() {
         " ",
         " ",
         " "
      };

      Console.Write("Initial list with empty strings...
");       foreach (string list in myList) {          Console.WriteLine(list);       }       Console.Write("Removing an empty element from the list...
");       myList.RemoveAt(0);       foreach (string list in myList) {          Console.WriteLine(list);       }       Console.WriteLine("Empty List after deleting an empty element is shown above...");    } }

Output

Initial list with empty strings...

Removing an empty element from the list...

Empty List after deleting an empty element is shown above...

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

717 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements