Array.Exist () only recognizes the last element of an array

I apologize if this is an obvious answer, but I couldn't find anything similar to this behavior on StackOverflow or Google. I am teaching C # myself and I am writing a program that involves the use of name-password / password pairs. In this case I am using .Exist()

to determine if the username is already in my array of usernames.

    static bool LookUpS(string targ, string[] arr)
    {
        if (Array.Exists(arr, e => e == targ))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

      

The argument targ

comes from the user, and the argument is arr

written to the program to indicate which array to look for (which means I can use LookUpS

for different arrays). The array I'm testing right now is from a text document. I have tested to verify that going from .txt to array works correctly.

Now to the actual problem. I filled in the text document in question with several values: "Jupiter", "Neptune", "Saturn", "Mars". If I pass any of the first three to LookUpS

, it returns false

even though they exist. The part I don't understand, however, is that if I pass "Mars" (or whatever the last value) to the function, it returns true

as I would expect. If I removed the value "Mars", LookUpS

I would say that "Saturn" true

, but not the rest. Can anyone suggest some explanation for this? I can post a more complete picture of the program if it helps identify the problem.

+3


source to share


3 answers


You mention in the comments that you split the lines with \ n as a delimiter separator. But on Windows \ r \ n is used. Hence your splitted string array will contain

  • xxxx \ r
  • yyyy \ y
  • lastUser


The last line does not contain a newline that will work.

This explains why your array search only finds the last user in your array. Go to the split operation not \ r, but Environment.NewLine.ToCharArray () to remove all newlines in the string array.

0


source


Arrays have a built-in method calledcontains()

, which should do exactly what you want ...



string[] array = { "Jupiter", "Neptune", "Saturn", "Mars" };
if (array.Contains("Neptune")) {
    return true;
}

      

0


source


You could simplify it to something like this. You can use Exists () instead of Any () if you like.

static bool LookUpS(string targ, string[] arr)
    {
        return (arr.Any(s => s == targ))
    }

      

Or, if you want it to be case insensitive:

static bool LookUpS(string targ, string[] arr)
    {
        return (arr.Any(e => String.Equals(e, targ, StringComparison.CurrentCultureIgnoreCase));
    }

      

Or, as Karl suggests, you can use Contains ()

static bool LookUpS(string targ, string[] arr)
        {
            return (arr.Contains(targ);
        }

      

Sometimes simplifying the code can solve some problems :)

The reason you don't need to use if .. else

to return true

or false

is because the return methods Any (), Exists () and Contains () are bool, so you can just return the method call as shown in the examples

0


source







All Articles