Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, June 25, 2016

Using the

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


is there anyway of fixing this statement

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

it comes up with the error

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

if anyone can help it would be much appreciated

Answer by Jeroen Vannevel for Using the "or" operator with strings in an "if" statement


You have to explicitly reference the first variable every time.

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

What you could do is create a collection and add all your separate strings to that. Afterwards you can use .Contains() on it.

Answer by Sudhakar Tillapudi for Using the "or" operator with strings in an "if" statement


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

or you can write a seperate function to do the check

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

Answer by Soner Gnl for Using the "or" operator with strings in an "if" statement


First of all, I want to point the root of your problem;

From || Operator (C# Reference)

The conditional-OR operator (||) performs a logical-OR of its bool operands.

So you can't use string values with || operator. Both operands should be boolean value.

You can use LINQ with Enumerable.Any as well like;

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

Answer by Esteban Elverdin for Using the "or" operator with strings in an "if" statement


For avoiding so many comparisons you can do

var list = new string[] {"hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"};    if (list.Contains(answer))  {  }  

Answer by harold for Using the "or" operator with strings in an "if" statement


Most programming languages don't work like that. You are actually asking it to compute "hello" || "Hello" || etc.. and the || operator can not be applied to strings (what would you want the result to be, anyway?)

That the expression appears as the right hand side of a comparison is irrelevant. Consider also what would happen if that wasn't the case - what would this mean?

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

Should that

  1. always execute, because someBool is always either true or false, or
  2. execute only if someBool is true, because true || false is true?

Every programming language I know of (in which this is applicable) chooses the second case, and that can be generalized to other constructs that look similar (such as your example) and even more generally, looking at any sub-expression in isolation is enough to determine what it does. Choosing the first way makes it so that you have to look at a whole expression before you can determine what any of its sub-expressions might means, because their meaning may depend on something external to them.

Answer by Baldrick for Using the "or" operator with strings in an "if" statement


One possible syntax is to create an extension method as follows:

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

Then you can call like this:

if (sAnswer.EqualsAny("hello", "Hello", "hi", "Hi", "hey", "Hey", "Hay", "hey"))  {       ...  }  

Answer by Andrei Alexandru Agape for Using the "or" operator with strings in an "if" statement


use .Equals() to compare strings

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

if you don't want to do a case sensitive comparison you can uppercase you sAnswer, this way you don't have to compare with multiple variants of the same string

        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  

Also, be careful if you use the contains() method:

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

will return you true (because watcHIng contains "hi") and i don't think you want that

Answer by christiandev for Using the "or" operator with strings in an "if" statement


You could also do:

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

using linq, and you could set the options to uppercase, and .ToUpper() the answer

Answer by HappyHuman for Using the "or" operator with strings in an "if" statement


Actually what you can do here is:

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

Compared to other suggestions, this could be the easiest way to code it, but some might think that this isn't a good practice. Anyway, have fun coding.

Answer by terrybozzio for Using the "or" operator with strings in an "if" statement


You can also declare a const string with all those values and each time you need to check just call contains method:

private const string test = "HellohelloHihiHeyhey";    static void Main(string[] args)  {      string UserInput = Console.ReadLine();        if (test.Contains(UserInput))      {          Console.WriteLine("success!!");      }     }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.