[英]Search for word and get whole string
我有一个如下所示的列表框:
DANAHER -- 190,00 € -- 20 PCS -- 12:51:52.361
DANAHER -- 190,00 € -- 270 PCS -- 12:51:57.361
RENAULT -- 37,815 € -- 23 PCS -- 12:51:52.475
RENAULT -- 37,815 € -- 1 PCS -- 12:51:59.475
CAMECO -- 13,854 € -- 20 PCS -- 12:53:13.028
CAMECO -- 13,854 € -- 29 PCS -- 12:53:53.028
SAPSE -- 105,48 € -- 20 PCS -- 12:53:22.631
SAPSE -- 105,48 € -- 80 PCS -- 12:53:27.631
当然,现在每秒钟就有新公司不断地添加到列表框中。 我想创建一个文本框,我可以在其中输入公司名称,如果存在条目,它会搜索我的列表框,如果存在,它应该获取字符串的 rest 并将其保存在某处
我的文本框示例
DANAHER RENAULT
所需的 output 应为:
DANAHER -- 190,00 € -- 20 PCS -- 12:51:52.361
DANAHER -- 190,00 € -- 270 PCS -- 12:51:57.361
RENAULT -- 37,815 € -- 23 PCS -- 12:51:52.475
RENAULT -- 37,815 € -- 1 PCS -- 12:51:59.475
到目前为止我已经尝试过:
string[] words = richTextBox2.Text.Split(' ');
var toCheck = words;
var newList = listwithentrys.Where(x => toCheck.Any(check => x.Contains(check)));
List<String> selectedCollection = newList.ToList();
listBox4.DataSource = selectedCollection;
但似乎无缘无故无法正常工作。
由于listwithentrys
是ListBox object,因此您需要先获取行。
然后,您需要检查这些行是否以words
数组中的确切字符串开头。 由于它们后面都有一个空格,因此您可以简单地检查该行是否以单词+空格开头。
var newList = listwithentrys.Items
.Cast<string>()
.Where(x =>
toCheck.Any(check => x.Trim().StartsWith($"{check} "))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.