40

私はこれを持っています:

List<string> s = new List<string>{"", "a", "", "b", "", "c"};

コードが見苦しくなるため("")、ステートメントを使用せずに (おそらく LINQ を使用して)空の要素をすべてすばやく削除したいと考えています。foreach

4

4 に答える 4

65

使用できますList.RemoveAll

C#

s.RemoveAll(str => String.IsNullOrEmpty(str));

VB.NET

s.RemoveAll(Function(str) String.IsNullOrEmpty(str))
于 2013-01-13T22:45:21.880 に答える
13

メソッドでチェックアウトしList.RemoveAllます。String.IsNullOrEmpty()

指定された文字列がnullであるか空の文字列であるかを示します。

s.RemoveAll(str => string.IsNullOrEmpty(str));

これがDEMOです。

于 2013-01-13T22:46:39.423 に答える
10
s = s.Where(val => !string.IsNullOrEmpty(val)).ToList();
于 2013-01-13T22:46:09.753 に答える