View Full Version : C# Or in string
patrickcabenjamin
11 Apr 2009, 10:04am
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.
BLuKnight
12 Apr 2009, 6:39am
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!
Nemikan
12 Apr 2009, 8:07am
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
}
patrickcabenjamin
12 Apr 2009, 11:49am
thanks very much
pragtastic
12 Apr 2009, 12:17pm
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 (http://msdn.microsoft.com/en-us/library/6a71f45d(VS.80).aspx), | (http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.80).aspx), and || (http://msdn.microsoft.com/en-us/library/6373h346(VS.80).aspx).
Best of luck with your C# adventures!
patrickcabenjamin
12 Apr 2009, 1:51pm
thanks a lot
but how about "or" for more than two different things which is not just about which case it is in?
:-)
pragtastic
12 Apr 2009, 2:17pm
You'll probably want to look into a switch statement (http://msdn.microsoft.com/en-us/library/06tc147t(VS.80).aspx) for that sort of thing.
patrickcabenjamin
12 Apr 2009, 2:19pm
how about an and in string
for example
if (name.Equals("Bob") (//and) home.Equals("london"))
{
//do something here
}</pre>
pragtastic
12 Apr 2009, 2:58pm
You'll want to use the And operator: &&
patrickcabenjamin
13 Apr 2009, 7:12pm
Thankyou
;-)
shwaip
13 Apr 2009, 11:14pm
does C# have .EqualsIgnoreCase()?
pragtastic
14 Apr 2009, 5:20am
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.
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.