Max
Max

Reputation: 13328

Removing empty bytes from List<byte>

How do I delete empty bytes from a List<byte>?

For example I got a list with a size of [5].

[0] = 5
[1] = 3
[2] = 0
[3] = 0
[4] = 17

At this example I want to delete byte with index: 2 and 3.

The Items in the list change every second. So the next time the list could be filled with something like:

[0] = 0
[1] = 2
[2] = 3
[3] = 4
[4] = 0

Upvotes: 2

Views: 1482

Answers (3)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

How about using List.RemoveAll() method?

Removes all the elements that match the conditions defined by the specified predicate.

YourList.RemoveAll(n => n == 0);

For example;

List<int> list = new List<int>(){5, 3, 0, 0, 17};
list.RemoveAll(n => n == 0);

foreach (var i in list)
{
    Console.WriteLine(i);
}

Output will be;

5
3
17

Here is a DEMO.

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109547

bytes.RemoveAll(x => x == 0)

Upvotes: 5

Rawling
Rawling

Reputation: 50104

It's something like

myList.RemoveAll(b => b == 0);

Upvotes: 7

Related Questions