Syntax question and String Comparison

  • Thread starter Thread starter Dinsdale
  • Start date Start date
D

Dinsdale

I would like to compare a string value to a pre-determined list of
other strings. Is there a simple way to do this in one statements like
this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what this
statement really is saying) but I'd like to be able to do *something*
like this. One suggestion was to add the compare strings to an
arraylist and do a binary search.

It's sad that my knowledge of and/or operators is so lacking. If anyone
knows of a good link to explain the difference between &&, &, ||, | i
would be greatly appreciative. I don't know how I have survived
without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}

Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))


Any info would be helpful. I don't mind doing the heavy lifting, I just
need a bit of direction.

Cheers,

Russ
 
The difference here is the difference between logical operators and bitwise
operators.

&& and || are logical operators.
& and | are bitwise operators.

A logical operator is used to evaluate an expression, and returns true or
false. The AND operator indicates that the operands on both sides MUST BOTH
evaluate to true. The OR operator indicates that only ONE of the operands
must be true (one OR the other)

X == 1 && X ==2 - False always, since X cannot logically be both 1 AND 2.
Y == 1 && Y < 2 - True, if Y is equal to 1, because 1 is logically less than
2. False if Y is equal to 0, because although 0 is less than 2, 0 is not
logically equal to 1, and thus BOTH can not be true, unless Y is equal to 1.

X < 2 || X > 2 - True, unless X is equal to 2, since 2 is neither less than
2 nor is it greater than 2
X == Y || X != Y True always. If X equals Y, the first part is true. If X is
not equal to Y, the second part is true. Therefore, one OR the other will
always be true.

A bitwise operator is actually a mathematical operator that combines 2
binary values to create a third. the way that this is done is that each bit
in a value is compared with the bit at the same position in another value,
and depending on the operator used, yeilds a 1 or a 0. The resulting value
is the value of the resulting binary number.

Bitwise operators are a "bit" more difficult to understand. Here's a good
reference, with examples:

http://en.wikipedia.org/wiki/Bitwise_operation

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
 
Actually, | and & are also logical operators. They are overloaded to serve
as both bitwise operators and non-short-circuit logical operators (identical
behavior to the VB And/Or operators).
For a more complete discussion, see
http://www.tangiblesoftwaresolutions.com/Articles/Logical Operator Equivalents in VB and CSharp.htm
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
 
I would like to compare a string value to a pre-determined list
of other strings. Is there a simple way to do this in one
statements like this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what
this statement really is saying) but I'd like to be able to do
*something* like this. One suggestion was to add the compare
strings to an arraylist and do a binary search.

Russ,

That's a good suggestion. Another way to search a small number
of strings is to use the System.String.IndexOf method:

if ("string_1|string_2|string_3".IndexOf(strMystring.ToLower()) >= 0)
// string found.
It's sad that my knowledge of and/or operators is so lacking. If
anyone knows of a good link to explain the difference between
&&, &, ||, | i would be greatly appreciative. I don't know how I
have survived without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}

The && operator is for logical "and" operations, as in:

if ((a == 1) && (b == 2))...

The || operator is the logical "or".

The & operator is used for bitwise "and" operations. It's almost
always used to test a number to see if one or more bits in that number
are set to 1.

For example:

// Two eight-bit numbers.
int a = 191; // Binary 10111111
int b = 128; // Binary 10000000

// Does "a" have its left-most bit turned on?
if ((a & b) == b)
// Yes it does.
else
// No it doesn't.
Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK |
Win32Com.EV_RLSD |
Win32Com.EV_RING |
Win32Com.EV_ERR))

The | operator is the bitwise "or" operator. It's usually used
to turn one or more bits on in a number.

For example:

int a = 1; // Binary 0001
int b = 2; // Binary 0010
int c = 4; // Binary 0100
int d = 8; // Binary 1000

// Create a number with the fourth and second bits turned on.
// (NB: The most significant bits are on the left, and the least
// significant bits are on the right. That's why the right-most bit
// is referred to as the first bit).
int e = b | d; // Binary 1010. The bitwise combination of 1000 and 0010.

// Use the & operator to see if a bit is set in e:
if ((e & b) == b) then
// Yes, it's set.

The & and | operators also work with multiple bits, not just the single
bits I've shown in the example code above.

It should be noted that bitwise "or-ing" numbers together is not
a form of addition. 8 | 8 == 8, but 8 + 8 == 16.
 
Chris R. Timmons said:
if ("string_1|string_2|string_3".IndexOf(strMystring.ToLower()) >= 0)
// string found.

Or this, in order to avoid matching substrings:

if ("string_1|string_2|string_3|".IndexOf(strMystring.ToLower() + "|") >=
0)
// string found.
 
See my other reply: & and | are identical in behavior to VB's 'And' & 'Or' -
they are overloaded to also act as non-short-circuiting logical operators
(operating on bool values).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
 
Wow. Thanks for the great response everybody. It's bitwise operations
that I need to work on. Thank you to Chris for that explaination. I'll
surmize that the following statement is sending the bitwise result of
all the operands?

if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK |
Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))

And I'm also going to guess that you can't use a bitwise operator on
strings because strings are unicode?

I decided to try the binary search idea, which turned out something
like this:

stirng strValue = "String3";
ArrayList alList = new ArrayList(new string[]
{"STRING1","STRING2","STRING3"});
alList.Sort();
if(alList.BinarySearch(strValue) > -1)
{
....
}

Cheers!
Russ
 
Or this, in order to avoid matching substrings:

if ("string_1|string_2|string_3|".IndexOf(strMystring.ToLower() + "|") >=
0)
// string found.

I should have said

if ("|string_1|string_2|string_3|".IndexOf("|" + strMystring.ToLower() +
"|") >= 0)
// string found.
 
Greg Young said:
| is a bitwise operator OR
http://en.wikipedia.org/wiki/Bitwise_operations#OR this operation is
intended for numeric values (not for strings)

|| is a logical OR http://en.wikipedia.org/wiki/Logical_or

Whoever told you to use the arraylist gave you a very good plan for this
case (especially with large numbers of strings).

That's not really a particularly good plan. A far better plan would be
to use a dictionary, where the hashcode is used for coarse searching,
giving a list of strings involved with the same hashcode. That list
will usually be very small.
 
Mark Wilden said:
I should have said

if ("|string_1|string_2|string_3|".IndexOf("|" + strMystring.ToLower() +
"|") >= 0)
// string found.

Unless, of course, strMystring is "string_1|string_2" in which case it
fails.

It's much simpler just to do three comparisons than to try to get
clever with this kind of thing, IMO.
 
Dinsdale said:
I'll
surmize that the following statement is sending the bitwise result of
all the operands?

if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK |
Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))
Right.

And I'm also going to guess that you can't use a bitwise operator on
strings because strings are unicode?

Right again.

Well, actually it's because there is no bitwise operator defined for
strings, as it wouldn't make any sense to perform bitwise operations on
strings.
 
Actually Jon it is a trade off. The hash index would use roughly 2.3 times
the memory as the binary search.

The binary search method can also do other functionalities such as "closest
match".

So it really depends which is important to you.

Cheers,

Greg Young
 
Greg Young said:
Actually Jon it is a trade off. The hash index would use roughly 2.3 times
the memory as the binary search.

Of the index, yes. I suspect that the bulk of the memory would usually
be taken by the strings themselves, unless you're talking about
thousands of very short strings.
The binary search method can also do other functionalities such as "closest
match".

So long as you view "AXXXXXXXXXX" as closer to "AHHHHHHHHHHHHHHHH" than
"ZXXXXXXXXXXXXXXX" :)
So it really depends which is important to you.

What is normally most important to me is code simplicity. Using a
dictionary gives the required functionality for free. No need to sort
things or use a binary search - just ask the dictionary whether or not
it contains the value, using the Contains method. Very simple.
 
Unless, of course, strMystring is "string_1|string_2" in which case it
fails.

Of course. I took it as read that you'd choose a separator that couldn't
occur in a string.
It's much simpler just to do three comparisons than to try to get
clever with this kind of thing, IMO.

Actually, the technique I mentioned isn't particularly "clever"; it's been a
standard technique for at least the 20 years I've been in the biz.

I'm not suggesting it's the best way for all applications, but it is "a
way," that's worked for a long time. I was just mentioning it as I was
surprised to see all this talk about logical and bitwise operators, when it
didn't seem anyone understood what was really being suggested to the OP. :)
 
Actually, | and & are also logical operators. They are overloaded to

That's just a matter of which semantics you're using, which is generally
dependent upon what you're doing with them. They are actually, like anything
else in a computer processor, mathematical operators. I found the semantics
I used to be helpful is expressing the difference between their general
usage.

You say "tomato" and I say "potato." But in the end, it is still a
grapefruit, by any other name. ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
 
I don't agree (unless I don't understand..)
| and & serve two purposes:
1. Bitwise operators on integer types.
2. Non-short-circuiting logical operators on bool types.

You can't just pick one - you have to understand that these operators are
overloaded to serve two very different purposes.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
 

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

Back
Top