简体   繁体   中英

Checking if string exists in List C#

I've been coding a program that stores student data (name and age) in a .txt file. I am now doing the delete method. However, when the user enters a string, I want it to compare the input to the strings in my List<string> , that's full of names. Code:

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

As you can see, the program stores inputSel into input (ToLower()), then compares each string in studentNames List<string> , and if it finds a match it flips the foundString bool , but even if I enter a name that matches (for example, it says JacobMusterson, I enter JacobMusterson, it should skip the "student not found", but it doesn't.

you should use input not inputSel

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

because of line:

string input = inputSel.ToLower();

where you are assinging to input lower version of inputSel

I suggest you to use IngonreCase in string.Compare for not making ToLower()

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

it will return 0 if equal see here

EDIT:

personally I would use:

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

您可以这样简单地做到这一点:

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

It will be more readable and effective if you will use Contains method of 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;
    }  

can be rewrite:

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

Just a small comment on your foreach-loop. You always iterating through all entries in the loop also if you already found out that the string is in the collection.

You can improve the performance of your code by replacing the last foreach.

Try:

bool foundString = studentNames.Select(file => file.ToLower()).Any(tempString => inputSel == tempString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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