简体   繁体   English

搜索单词并获取整个字符串

[英]Search for word and get whole string

I have a listbox which looks like follows:我有一个如下所示的列表框:

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

Now of course every second new companys are being added to the listbox, continously.当然,现在每秒钟就有新公司不断地添加到列表框中。 I wanted to create a textbox, where I can put in names of companys and it searchs through my listbox if a entry exists, if one exists it shoud get the rest of the string and save it somewhere我想创建一个文本框,我可以在其中输入公司名称,如果存在条目,它会搜索我的列表框,如果存在,它应该获取字符串的 rest 并将其保存在某处

Example of my textbox我的文本框示例

DANAHER RENAULT

Desired output shall be:所需的 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

What I have tried so far:到目前为止我已经尝试过:

   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;

But doesnt seem to work properly for no real reason.但似乎无缘无故无法正常工作。

Since listwithentrys is a ListBox object, you need to get the lines first.由于listwithentrysListBox object,因此您需要先获取行。

Then, you need to check if the lines start with an exact string from the words array.然后,您需要检查这些行是否以words数组中的确切字符串开头 Since they all are followed with a space, you may simply check if the line starts with the word + a space.由于它们后面都有一个空格,因此您可以简单地检查该行是否以单词+空格开头。

var newList = listwithentrys.Items
    .Cast<string>()
    .Where(x => 
        toCheck.Any(check => x.Trim().StartsWith($"{check} "))
    )

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

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