Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 빈 문자열 목록에서 빈 문자열을 제거하는 방법은 무엇입니까?

<시간/>

먼저 빈 문자열을 요소로 사용하여 목록을 설정합니다.

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

이제 인덱스를 사용하여 하나의 빈 요소를 제거하겠습니다.

myList.RemoveAt(0);

예시

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...");
   }
}

출력

Initial list with empty strings...

Removing an empty element from the list...

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