私はこれを持っています:
List<string> s = new List<string>{"", "a", "", "b", "", "c"};
コードが見苦しくなるため("")
、ステートメントを使用せずに (おそらく LINQ を使用して)空の要素をすべてすばやく削除したいと考えています。foreach
使用できますList.RemoveAll
:
C#
s.RemoveAll(str => String.IsNullOrEmpty(str));
VB.NET
s.RemoveAll(Function(str) String.IsNullOrEmpty(str))
メソッドでチェックアウトしList.RemoveAll
ます。String.IsNullOrEmpty()
指定された文字列がnullであるか空の文字列であるかを示します。
s.RemoveAll(str => string.IsNullOrEmpty(str));
これがDEMO
です。
s = s.Where(val => !string.IsNullOrEmpty(val)).ToList();