0

我一直在编写一个将学生数据(姓名和年龄)存储在 .txt 文件中的程序。我现在正在做删除方法。但是,当用户输入一个字符串时,我希望它将输入与 my 中的字符串进行比较List<string>,其中充满了名称。代码:

    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;
    }  

如您所见,程序存储inputSelinput(ToLower()) 中,然后比较 studentNamesList<string>中的每个字符串,如果找到匹配项,它会翻转 foundString bool,但即使我输入了匹配的名称(例如,它说 JacobMusterson,我输入 JacobMusterson,它应该跳过“找不到学生”,但它没有。

4

5 回答 5

2

你应该使用 input 而不是 inputSel

if (input == tempString)
{
    foundString = true;
}

因为线:

string input = inputSel.ToLower();

您在哪里输入较低版本的 inputSel

我建议你在 string.Compare 中使用 IngonreCase 来避免 ToLower()

var b =  string.Compare("a","A",StringComparison.OrdinalIgnoreCase);

如果相等,它将返回 0 参见此处

编辑:

我个人会使用:

var exists = studentNames.Any(x=>string.Compare(x,inputSel,StringComparison.OrdinalIgnoreCase)==0);
于 2013-10-30T07:47:49.827 回答
1

你可以简单地这样做:

Boolean foundString = studentNames.Exists(name => name.ToLower().Equals(input));
于 2013-10-30T07:57:29.583 回答
1

为什么不简单地使用,

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

List<String>list和 s在哪里String

于 2021-05-20T06:46:54.820 回答
0

Contains如果您使用以下方法,它将更具可读性和有效性List

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;
}
于 2013-10-30T07:53:09.040 回答
0

只是对你的 foreach 循环的一个小评论。如果您已经发现字符串在集合中,您也总是遍历循环中的所有条目。

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

尝试:

bool foundString = studentNames.Select(file => file.ToLower()).Any(tempString => inputSel == tempString);
于 2013-10-30T07:54:14.287 回答