Comparing a string and array

Joined
Jul 28, 2021
Messages
4
Reaction score
0
I am new to c# is there any simple way to compare the user's input to an array for an authentication system to a game?
thanks
 

Attachments

  • Screenshot 2021-07-28 224609.png
    Screenshot 2021-07-28 224609.png
    4.1 KB · Views: 57
  • Screenshot 2021-07-28 224558.png
    Screenshot 2021-07-28 224558.png
    3.9 KB · Views: 56
Joined
Mar 3, 2021
Messages
240
Reaction score
30
The problem appears to be that one of your strings is a string array (string[]). It's hard to say without seeing the actual code. But, the following is how you compare two strings.
C#:
string str1 = "Hello World!";
string str2 = "Hello World!";
if (str1 == str2)
{
    Console.WriteLine("equal");
} else
{
    Console.WriteLine("not equal");
}
 
Joined
Jul 28, 2021
Messages
4
Reaction score
0
The problem appears to be that one of your strings is a string array (string[]). It's hard to say without seeing the actual code. But, the following is how you compare two strings.
C#:
string str1 = "Hello World!";
string str2 = "Hello World!";
if (str1 == str2)
{
    Console.WriteLine("equal");
} else
{
    Console.WriteLine("not equal");
}
What i an trying to do is make a authentication system where you input a name and it looks at a array and sees if they can go in or not. When i try doing it like you have said it says it cant convert a string[] to a string
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
By using the System.Linq namespace, arrays get a Contains function that can find a value inside them. At the top of the program, add
C#:
using System.Linq;
Then, we change our comparison:
C#:
string[] usernames = new string[]{ "user1", "user2" };
string user = "user1";
if (usernames.Contains(user))
{
    Console.WriteLine("user found");
} else
{
    Console.WriteLine("user not found");
}
 
Joined
Jul 28, 2021
Messages
4
Reaction score
0
By using the System.Linq namespace, arrays get a Contains function that can find a value inside them. At the top of the program, add
C#:
using System.Linq;
Then, we change our comparison:
C#:
string[] usernames = new string[]{ "user1", "user2" };
string user = "user1";
if (usernames.Contains(user))
{
    Console.WriteLine("user found");
} else
{
    Console.WriteLine("user not found");
}
I am now getting this error when trying to put “using system.linq
 

Attachments

  • image.jpg
    image.jpg
    200.8 KB · Views: 56
Joined
Mar 3, 2021
Messages
240
Reaction score
30
I don't know that error off the top of my head, but you have to put the using statement way up at the top of the file.
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
The `using System.Linq` goes up at the top to import functions/etc so that they can be used. When it's imported, arrays get a Contain method for finding values within them. Below is my entire program.
C#:
using System;
using System.Linq;

namespace Stringcompare
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] usernames = new string[]{ "user1", "user2" };
            string user = "user1";
            if (usernames.Contains(user))
            {
                Console.WriteLine("user found");
            } else
            {
                Console.WriteLine("user not found");
            }
        }
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top