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

輸出

Initial list with empty strings...

Removing an empty element from the list...

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

更新於:22-Jun-2020

1K+ 瀏覽量

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.