我如何过滤列表字符串 [英] How I filter list string
本文介绍了我如何过滤列表字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下所示的CAN msg ID的字符串列表
CF00400
CFE6CEE
18F0010B
CFFB027
如果消息ID以27结尾.我需要从列表中删除消息ID.我该怎么办??
谢谢
John
Hi,
I have list string of CAN msg IDs like below
CF00400
CFE6CEE
18F0010B
CFFB027
If the msg id ends with 27. I need to delete the message ID from the list. How can I do it???
Thanks
John
推荐答案
List<string> s = new List<string>();
s.RemoveAll( i => i.EndsWith("27"));
您还可以使用基于循环和索引的数组来删除项目.
you can also use for loop and index based array to remove item.
List<string> msgIds = new List<string>()
{
"CF00400","CFE6CEE","18F0010B","CFFB027"
};
int totalItems = msgIds.Count;
for (int i = 0; i < totalItems; i++)
{
if (msgIds[i].Substring(msgIds[i].Length - 2, 2).Equals("27"))
{
msgIds.Remove(msgIds[i]);
}
}
foreach (var item in msgIds)
{
Response.Write(item + "<br/>");
}
像这样尝试...
try like this...
List<string> lst = new List<string>();
lst.Add("CF00400");
lst.Add("CFE6CEE");
lst.Add("18F0010B");
lst.Add("CFFB027");
lst = lst.Where(k => (!k.EndsWith("27"))).ToList();
这篇关于我如何过滤列表字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文