Computer >> Máy Tính >  >> Lập trình >> C#

Làm cách nào để xóa một chuỗi trống khỏi danh sách các chuỗi trống trong C #?

Đầu tiên, đặt một danh sách với chuỗi trống làm phần tử.

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

Bây giờ chúng ta hãy xóa một phần tử trống bằng chỉ mục.

myList.RemoveAt(0);

Ví dụ

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...\n");
      foreach (string list in myList) {
         Console.WriteLine(list);
      }

      Console.Write("Removing an empty element from the list...\n");
      myList.RemoveAt(0);

      foreach (string list in myList) {
         Console.WriteLine(list);
      }
      Console.WriteLine("Empty List after deleting an empty element is shown above...");
   }
}

Đầu ra

Initial list with empty strings...

Removing an empty element from the list...

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