Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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中是否存在字符串#_C#_List_Foreach - Fatal编程技术网

C# 检查列表C中是否存在字符串#

C# 检查列表C中是否存在字符串#,c#,list,foreach,C#,List,Foreach,我一直在编写一个程序,将学生数据(姓名和年龄)存储在.txt文件中。我现在正在执行delete方法。但是,当用户输入字符串时,我希望它将输入与我的列表中的字符串进行比较,该列表中的字符串都是全名。代码: string tempFileName; string inputSel; // Selection string for delete Console.WriteLine(" -- Deleting Grade {0} -- ", grade); Console

我一直在编写一个程序,将学生数据(姓名和年龄)存储在.txt文件中。我现在正在执行delete方法。但是,当用户输入字符串时,我希望它将输入与我的
列表中的字符串进行比较,该列表中的字符串都是全名。代码:

    string tempFileName;
    string inputSel; // Selection string for delete
    Console.WriteLine(" -- Deleting Grade {0} -- ", grade);
    Console.WriteLine("- Enter a student name to delete: ");
    foreach (string file in fileNames)
    {
        tempFileName = file.Replace(".txt", "");
        studentNames.Add(tempFileName);
    }
    foreach (string name in studentNames)
    {
        Console.Write("{0}\n", name);
    }
    Console.WriteLine();
    Console.Write("> ");
    inputSel = Console.ReadLine();
    string input = inputSel.ToLower();
    string tempString;
    bool foundString = false;
    foreach (string file in studentNames)
    {
        tempString = file.ToLower();
        if (inputSel == tempString)
        {
            foundString = true;
        }
    }
    if (!foundString)
    {
        Console.WriteLine("Wrong name entered!");
        Console.WriteLine("Returning to grades menu..");
        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
        return;
    }  

如您所见,程序将
inputSel
存储到
input
(ToLower())中,然后比较studentNames
列表中的每个字符串,如果找到匹配项,则会翻转foundString
bool
,但即使输入匹配的名称(例如,它说jacomusterson,我输入jacomusterson,它也应该跳过“找不到学生”,但找不到。

您应该使用输入而不是输入选择

if (input == tempString)
{
    foundString = true;
}
由于线路:

string input = inputSel.ToLower();
您正在协助输入较低版本的inputSel

我建议您在string中使用IngonreCase.Compare来表示不使用ToLower()

如果相等,它将返回0

编辑:

我个人会使用:

var exists = studentNames.Any(x=>string.Compare(x,inputSel,StringComparison.OrdinalIgnoreCase)==0);

您应该使用输入而不是输入选择

if (input == tempString)
{
    foundString = true;
}
由于线路:

string input = inputSel.ToLower();
您正在协助输入较低版本的inputSel

我建议您在string中使用IngonreCase.Compare来表示不使用ToLower()

如果相等,它将返回0

编辑:

我个人会使用:

var exists = studentNames.Any(x=>string.Compare(x,inputSel,StringComparison.OrdinalIgnoreCase)==0);

如果您使用
Contains
方法的
列表
,它将更具可读性和有效性:

foreach (string file in studentNames)
    {
        tempString = file.ToLower();
        if (inputSel == tempString)
        {
            foundString = true;
        }
    }
if (!foundString)
    {
        Console.WriteLine("Wrong name entered!");
        Console.WriteLine("Returning to grades menu..");
        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
        return;
    }  
可以重写:

if(!studentNames.Contains(inputSel, StringComparer.Create(CultureInfo.InvariantCulture, true)))
{
            Console.WriteLine("Wrong name entered!");
            Console.WriteLine("Returning to grades menu..");
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            return;
}

如果您使用
Contains
方法的
列表
,它将更具可读性和有效性:

foreach (string file in studentNames)
    {
        tempString = file.ToLower();
        if (inputSel == tempString)
        {
            foundString = true;
        }
    }
if (!foundString)
    {
        Console.WriteLine("Wrong name entered!");
        Console.WriteLine("Returning to grades menu..");
        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
        return;
    }  
可以重写:

if(!studentNames.Contains(inputSel, StringComparer.Create(CultureInfo.InvariantCulture, true)))
{
            Console.WriteLine("Wrong name entered!");
            Console.WriteLine("Returning to grades menu..");
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            return;
}

只要对foreach循环进行一个小注释。如果您已经发现字符串在集合中,那么您总是遍历循环中的所有条目

您可以通过替换最后一个foreach来提高代码的性能

尝试:


只要对foreach循环进行一个小注释。如果您已经发现字符串在集合中,那么您总是遍历循环中的所有条目

您可以通过替换最后一个foreach来提高代码的性能

尝试:


您可以这样做:

Boolean foundString = studentNames.Exists(name => name.ToLower().Equals(input));

您可以这样做:

Boolean foundString = studentNames.Exists(name => name.ToLower().Equals(input));
为什么不干脆用,

if(list.Contains(s)){
    //found
}else{
    //not found
}
其中list是
list
,s是
字符串

为什么不简单地使用

if(list.Contains(s)){
    //found
}else{
    //not found
}

其中list是
list
,s是
字符串

哇,我不敢相信我错过了。非常感谢!(为什么我不能接受答案?@Ilan321你可以,但你必须等几分钟,弹出窗口说哇,我不能相信我错过了。非常感谢!(为什么我不能接受答案?)@Ilan321你可以,但你必须等几分钟,弹出窗口说不要忘记接受有用的答案不要忘记接受有用的答案