Reputation: 13328
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
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