Using the "or" operator with strings in an "if" expression

in any case, this statement is recorded

if (sAwnser == ("hello" || "Hello" || "hi" || "Hi" || "hey" || "Hey" || "Hay" || "hey"))
{

}

an error

Operator '||' cannot be applied to operands of type 'string' and 'string'

if someone can help, he will be very grateful

+4
source share
10 answers

To avoid so many comparisons you can do

var list = new string[] {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"};

if (list.Contains(answer))
{
}
+11
source

You must explicitly reference the first variable each time.

sAwnser == "hello"returns a boolean value. You cannot compare boolean to string.

What you can do is create a collection and add all your individual lines to it. Subsequently, you can use .Contains()on it.

+8
source

, ;

|| Operator (C# Reference)

- (||) - bool.

, string ||. boolean.

LINQ Enumerable.Any, :

string[] array = new string[] {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"};
if (array.Any(sAwnser.Equals))
{
   // Your sAwnser is equal one of your array values.
}
+3

:

public static bool EqualsAny<T>(this T input, params T[] items)
{
    return items.Contains(input);
}

:

if (sAnswer.EqualsAny("hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"))
{ 
    ...
}
+3
if (sAwnser == "hello" || sAwnser == "Hello" || sAwnser == "hi" || sAwnser == "Hi" || sAwnser == "hey" || sAwnser =="Hey" || sAwnser =="Hay" || sAwnser =="hey"))
{

}

private bool CheckInput(String input)
        {
            String[] sAwnser = {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"};
            for (int i = 0; i < sAwnser.Length; i++)
            {
                if (sAwnser[i].Equals(input));
                return true;
            }
            return false;
        }
+2

:

var sAnswer = "hello";
var answerList = new List<string> { "HELLO", "HI", "HEY", "HAY" };
bool checkAnswer = answerList.Any(r => r == sAnswer.ToUpper()); 

linq, , .ToUpper()

+2

. "hello" || "Hello" || etc.., || ( , ?)

, , . , , - ?

if (someBool == (true || false))
    something;

  • , someBool true, false,
  • , someBool - true, true || false - true?

, ( ), , (, ) , , , . , , , , - .

+1

.Equals()

        String sAnswer = "hello";
        if( sAnswer.Equals("hello")|| sAnswer.Equals("Hi"))
            System.Console.Write("yes");
        else
             System.Console.Write("no");

, , ,

        String sAnswer1 = "hello";
        String sAnswer2 = "heLLo";
        String sAnswer3 = "HellO";
        sAnswer = sAnswer.ToUpper();
        if( sAnswer1.Equals("HELLO")) -> True
        if( sAnswer2.Equals("HELLO")) -> True
        if( sAnswer3.Equals("HELLO")) -> True

, , contains():

        String sAnswer = "watching";
        if( sAnswer.Contains("hi"))

( watcHIng "" ), , ,

0

, :

if ((sAwnser == "hello") || (sAwnser =="Hello") || (sAwnser =="hi") || (sAwnser =="Hi") || (sAwnser =="hey") || (sAwnser =="Hey") || (sAwnser =="Hay") || (sAwnser =="hey"))
{
 //insert code here
}

, , , . , .

0

const , , :

private const string test = "HellohelloHihiHeyhey";

static void Main(string[] args)
{
    string UserInput = Console.ReadLine();

    if (test.Contains(UserInput))
    {
        Console.WriteLine("success!!");
    }   
}
0

All Articles