简体   繁体   English

从List中删除空字节<byte>

[英]Removing empty bytes from List<byte>

How do I delete empty bytes from a List<byte> ? 如何从List<byte>删除空字节?

For example I got a list with a size of [5] . 例如,我得到了一个大小为[5]的列表。

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

At this example I want to delete byte with index: 2 and 3. 在这个例子中,我想删除索引的byte :2和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

就像这样

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

How about using List.RemoveAll() method? 如何使用List.RemoveAll()方法?

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 . 这是一个DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM