Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表框中搜索字符串_C#_String_String Comparison - Fatal编程技术网

C# 在列表框中搜索字符串

C# 在列表框中搜索字符串,c#,string,string-comparison,C#,String,String Comparison,我在列表框中查找字符串时遇到问题,我的字符串NombreCompleto由我以前从文件(ESSD)中读取的3个字符串组成,在恢复此字符串后,我想知道此字符串是否在我的列表框中3,我尝试了几种方法,但似乎不起作用。 这是我的密码 foreach (string h in Directory.EnumerateFiles(NomDirec, "resume*")) { this.listBox1.Items.Add(h); var NombreLinea = File.ReadLine

我在列表框中查找字符串时遇到问题,我的字符串NombreCompleto由我以前从文件(ESSD)中读取的3个字符串组成,在恢复此字符串后,我想知道此字符串是否在我的列表框中3,我尝试了几种方法,但似乎不起作用。 这是我的密码

foreach (string h in Directory.EnumerateFiles(NomDirec, "resume*"))
{
   this.listBox1.Items.Add(h);
    var NombreLinea = File.ReadLines(h);
    foreach (string item in NombreLinea)
    {
        NombreAbuscar.Add(item.Remove(item.IndexOf(':')));
        this.listBox3.Items.Add(item.Remove(item.IndexOf(':')));

}

foreach (string t in Directory.EnumerateFiles(NomDirec, "ESSD1*"))
{
    string[] Nombre = File.ReadLines(t).ElementAtOrDefault(6).Split(':');
    string[] ApellidoPat = File.ReadLines(t).ElementAtOrDefault(7).Split(':');
    string[] ApellidoMat = File.ReadLines(t).ElementAtOrDefault(8).Split(':');
    string NombreCompleto = ApellidoPat[1]+" "+ ApellidoMat[1] +","+" "+ Nombre[1];
    string Nom2 = NombreCompleto.ToString();

    int index = listBox3.FindString(Nom2);
    if (index != -1)
    {
        this.listBox1.Items.Add(t);
        MessageBox.Show("Find It");
    }
    else { MessageBox.Show("Not Found :@"); }
}

您可以尝试使用此代码-
基于Linq运算符,其中,…

var selectedItems = from li in listBox3.Items
                    where li.Text == Nom2
                    select li.Text;

if(selectedItems.Any()) 
.... 
试试这个:

                            int index = -1;
                            for (int i = 0; i < listBox3.Items.Count; ++i)
                               if (listBox3.Items[i].Text == Nom2) { index = i; break; }
                            if (index != -1)
                            {
                                this.listBox1.Items.Add(t);
                                MessageBox.Show("Find It");
                            }
                            else { MessageBox.Show("Not Found :@");
int索引=-1;
对于(int i=0;i
查找某个字符串是否在列表框中非常简单

private void btnAddRecipe_Click(object sender, EventArgs e)
{
    bool DoesItemExist = false;
    string searchString = txtRecipeName.Text;
    int index = lstRecipes.FindStringExact(searchString, -1);

    if (index != -1) DoesItemExist = true;
    else DoesItemExist = false;

    if (DoesItemExist)
    {
       //do something
    }
    else
    {
        MessageBox.Show("Not found", "Message", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop);
    }

    PopulateRecipe();
}

您可以使用
Any
而不是
Count()==0)
。它不仅能更清楚地表达意思,而且性能也会更好(它不需要重复整个序列,只需尝试获取第一项)。您还需要删除
ToList
,因为不需要急切地将整个序列迭代到列表中。您能解释一下吗,我在c#中是新手谢谢!carlos您有初始序列,您使用“from”迭代集合项,您使用“where”过滤,您使用“select”选择您的项ind是什么?where你申报了吗?谢谢!@carloscarbajal抱歉。我只是指
index