C# Or in string

hi

I am a complete programming noob. I have only recently started on c# and am terrible. I can write stuff and ask stuff and use strings to an extent.
I am looking for a way to write or in this sort of context

System.Console.Out.WriteLine("what is your name");
name = System.Console.Out.ReadLine();
if (name.Equals("Bob")
{
//do something here
}
//or
if (name.Equals("bob");
{
//do the same here
}

I want to merge the two together so i don't have to write the same thing out for each one.

Comments

  • BLuKnightBLuKnight Lehi, UT Icrontian
    edited April 2009
    Hi Patrick,

    Your best bet is to use the 'or' comparison. With this, you can join the two statements. So, you should be able to put:
    if (name.Equals("Bob") or name.Equals("bob"))
    {
    //do something here
    }
    

    However, this only covers two cases. What if they put in bOb? Then this doesn't catch it. So, I would try this:
    if (name.ToLower().Equals("bob")) {
    //do something here
    }
    

    C# is an awesome language. Good luck!
  • NemikanNemikan Icrontian
    edited April 2009
    also, if you were to use your original code look into using an else if for the second case. The reasoning is then it will only check the 2nd case if the first case is false, if the first case is true, it skips the 2nd case.

    System.Console.Out.WriteLine("what is your name");
    name = System.Console.Out.ReadLine();
    if (name.Equals("Bob")
    {
    //do something here
    }
    //or
    else if (name.Equals("bob");
    {
    //do the same here
    }
  • edited April 2009
    thanks very much
  • pragtasticpragtastic Alexandria, VA Icrontian
    edited April 2009
    BLuKnight wrote:
    if (name.Equals("Bob") or name.Equals("bob"))
    {
    //do something here
    }
    

    I'm not sure if the "or" operator is actually valid in C#, but I can say for certain that "||" is the common approach to making a conditional-OR statement.

    Such as:
    if (name.Equals("Bob") || name.Equals("bob"))
    {
    //do something here
    }
    

    The advantage to the conditional-OR is that it short-circuits the statement, so if the left side is true, the right side is never evaluated. MSDN has some basic references for Operators, |, and ||.

    Best of luck with your C# adventures!
  • edited April 2009
    thanks a lot

    but how about "or" for more than two different things which is not just about which case it is in?

    :-)
  • pragtasticpragtastic Alexandria, VA Icrontian
    edited April 2009
    You'll probably want to look into a switch statement for that sort of thing.
  • edited April 2009
    how about an and in string

    for example


    if (name.Equals("Bob") (//and) home.Equals("london"))
    {
    //do something here
    }</pre>
  • pragtasticpragtastic Alexandria, VA Icrontian
    edited April 2009
    You'll want to use the And operator: &&
  • edited April 2009
    Thankyou
    ;-)
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited April 2009
    does C# have .EqualsIgnoreCase()?
  • pragtasticpragtastic Alexandria, VA Icrontian
    edited April 2009
    Closest thing I can find is:
    string thing = "yourmom";
    if(thing.Equals("slewt", StringComparison.OrdinalIgnoreCase))
    {
      Console.Write("Not a shocker.");
    }
    else
    {
      Console.Write("Lies.");
    }
    

    StringComparison in this case is simply an enum that specifies the type of matching to do, either word or ordinal sorting and whether or not to take case into account.
Sign In or Register to comment.