如何在 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.