uzay95
uzay95

Reputation: 16622

How remove empty element from string array in one line?

string[] ssss = "1,2,,3".Split(new[] {','})
                  .Where(a=>!string.IsNullOrEmpty(a))
                  .Select();

How does this works?

Upvotes: 1

Views: 4698

Answers (4)

Kobi
Kobi

Reputation: 138017

You could also use

"1,2,,3".Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

Upvotes: 9

Thomas Levesque
Thomas Levesque

Reputation: 292415

string[] ssss = "1,2,,3".Split(new[] {','}).Where(a=>!string.IsNullOrEmpty(a)).ToArray();

Upvotes: 2

uzay95
uzay95

Reputation: 16622

var ssss = "1,2,,3".Split(new[] {','}).Where(a=>!string.IsNullOrEmpty(a));
foreach (string s in ssss)
{
    Console.WriteLine(s);
}

Upvotes: 1

bruno conde
bruno conde

Reputation: 48265

string[] ssss = "1,2,,3".Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries);

Upvotes: 3

Related Questions