473,785 Members | 2,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# - Test for Equality Among Several Strings

Hi All,

I've spent the last few hours looking for a simple yet clean method via
C# that will allow me to test whether three or more strings are equal
(by value, of course).

I've been forced to go with the obvious (but ugly, if you ask me)
method below:

string a = "test";
string b = "test";
string c = "test";
string d = "test";

if ((a == b) && (b == c) && (c == d))
{
// do something here
}

I'd simply like to be able to do something like this...

if (a == b == c == d)
{
// do something here
}

I'm aware that the order of operations with boolean operators won't
allow for the above statement, resulting in an invalid boolean-string
comparison at run-time.

Also, I don't want to go the route of putting these four strings into a
sorted array or a hash table as an alternate solution to this problem,
which are the only other solutions I've seen mentioned thus far. Those
two options are just plain overkill if you ask me... at least in my
case.

Does it not seem like there should be a more clever mechanism available
by which to handle this? Now I know I can write (or override) my own
function to handle this, but my mind keeps telling me there's a better,
smarter way about it. So... any ideas?

Also, I'd love to here if anyone out there knows of another language
(possibly Ruby?) that could perform a task like this with much more
style and ease. I'm definitely looking for a language that can offer
me more flexibility than C#.

Thanks a bunch!
Derek

Jan 5 '07 #1
4 1486
How about this? copes with any number and any type, optionally
allowing your own comparer (definition of equality)...

Marc

static void Main()
{
Console.WriteLi ne(AllEqual("ab c", "abc", "abc"));
Console.WriteLi ne(AllEqual("ab c", "def", "abc"));
Console.WriteLi ne(AllEqual(1, 1, 1, 1, 1));
Console.WriteLi ne(AllEqual(1, 2, 3, 2, 1));
}

static bool AllEqual<T>(par ams T[] values)
{
return AllEqual<T>(Equ alityComparer<T >.Default, values);
}
static bool AllEqual<T>(IEq ualityComparer< Tcomparer, params T[]
values)
{
if(values==null ) throw new ArgumentNullExc eption("values" );
if (comparer == null) throw new
ArgumentNullExc eption("compare r");
int len = values.Length;
if(len<2) throw new InvalidOperatio nException("2 or more
values required");

T val = values[0];
for (int i = 1; i < len; i++)
{
if (!comparer.Equa ls(val, values[i]))
{
return false;
}
}
return true;
}
Jan 5 '07 #2
What about a method with a paramarray ?

<dw********@gma il.comschrieb im Newsbeitrag
news:11******** *************@3 8g2000cwa.googl egroups.com...
Hi All,

I've spent the last few hours looking for a simple yet clean method via
C# that will allow me to test whether three or more strings are equal
(by value, of course).

I've been forced to go with the obvious (but ugly, if you ask me)
method below:

string a = "test";
string b = "test";
string c = "test";
string d = "test";

if ((a == b) && (b == c) && (c == d))
{
// do something here
}

I'd simply like to be able to do something like this...

if (a == b == c == d)
{
// do something here
}

I'm aware that the order of operations with boolean operators won't
allow for the above statement, resulting in an invalid boolean-string
comparison at run-time.

Also, I don't want to go the route of putting these four strings into a
sorted array or a hash table as an alternate solution to this problem,
which are the only other solutions I've seen mentioned thus far. Those
two options are just plain overkill if you ask me... at least in my
case.

Does it not seem like there should be a more clever mechanism available
by which to handle this? Now I know I can write (or override) my own
function to handle this, but my mind keeps telling me there's a better,
smarter way about it. So... any ideas?

Also, I'd love to here if anyone out there knows of another language
(possibly Ruby?) that could perform a task like this with much more
style and ease. I'm definitely looking for a language that can offer
me more flexibility than C#.

Thanks a bunch!
Derek

Jan 5 '07 #3
A simple example:

static bool Equals(params string[] values)
{
for (int i = 1; i < values.Length; i++)
{
if (values[0] != values[i]) return false;
}
return true;
}
"Christof Nordiek" <cn@nospam.desc hrieb im Newsbeitrag
news:e5******** ******@TK2MSFTN GP03.phx.gbl...
What about a method with a paramarray ?

<dw********@gma il.comschrieb im Newsbeitrag
news:11******** *************@3 8g2000cwa.googl egroups.com...
>Hi All,

I've spent the last few hours looking for a simple yet clean method via
C# that will allow me to test whether three or more strings are equal
(by value, of course).

I've been forced to go with the obvious (but ugly, if you ask me)
method below:

string a = "test";
string b = "test";
string c = "test";
string d = "test";

if ((a == b) && (b == c) && (c == d))
{
// do something here
}

I'd simply like to be able to do something like this...

if (a == b == c == d)
{
// do something here
}

I'm aware that the order of operations with boolean operators won't
allow for the above statement, resulting in an invalid boolean-string
comparison at run-time.

Also, I don't want to go the route of putting these four strings into a
sorted array or a hash table as an alternate solution to this problem,
which are the only other solutions I've seen mentioned thus far. Those
two options are just plain overkill if you ask me... at least in my
case.

Does it not seem like there should be a more clever mechanism available
by which to handle this? Now I know I can write (or override) my own
function to handle this, but my mind keeps telling me there's a better,
smarter way about it. So... any ideas?

Also, I'd love to here if anyone out there knows of another language
(possibly Ruby?) that could perform a task like this with much more
style and ease. I'm definitely looking for a language that can offer
me more flexibility than C#.

Thanks a bunch!
Derek


Jan 5 '07 #4
Hi,

<dw********@gma il.comwrote in message
news:11******** *************@3 8g2000cwa.googl egroups.com...
Hi All,

I've spent the last few hours looking for a simple yet clean method via
C# that will allow me to test whether three or more strings are equal
(by value, of course).

I've been forced to go with the obvious (but ugly, if you ask me)
method below:
Unless you have your values in a collection I do not see a better way

--
Ignacio Machin
machin AT laceupsolutions com
Jan 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1641
by: Thomas Moore | last post by:
Hi: I am confused at string identity test: Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a="test" >>> b="test" >>> a is b
8
4496
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working with weird stuff, just strings, int, bools and DataTime values) My fields values for this record, for convenience are also in an array of objects. Now I am trying to write code like the following. private void processData (object d, object a)...
6
1550
by: onetitfemme | last post by:
Hi *, I have been looking for a definition or at least some workable concept of "XML equality". Searching on "XML equality" in comp.text.xml, microsoft.public.xsl and microsoft.public.xml resulted in no hits I also searched for: XML equality schema (single words) on the same newsgroups gave very little and not-to-the-point links
27
3788
by: Josh | last post by:
We have a program written in VB6 (over 100,000 lines of code and 230 UI screens) that we want to get out of VB and into a better language. The program is over 10 years old and has already been ported from VB3 to VB6, a job which took over two years. We would like to port it to Python, but we need to continue to offer upgrades and fixes to the current VB6 version. Does anybody know of ways we could go about rewriting this, one screen at a...
7
1975
by: Gary Brown | last post by:
Hi, In C#, how do you determine two objects are the "same" rather than "equal?" In C/C++ you can check the addresses and LISP provides a rich set of equality operators but C# appears ambiguous. Search of the on-line documentation of "equal" and "same" yielded nothing useful. Thanks,
1
1437
by: Timothy Grant | last post by:
I'm playing around with py.test and writing a parser for it's output for use in TextMate. I've run into what appears to be a strange phenomenon, but which is likely me doing something wrong. I'm writing a test to test some HTML output and the test fails for several reasons, all of which I understand but one. Here's the output (I've surrounded the problem area with *** to make
15
10870
by: Sandra-24 | last post by:
Comparing file system paths as strings is very brittle. Is there a better way to test if two paths point to the same file or directory (and that will work across platforms?) Thanks, -Sandra
4
1658
by: stj911 | last post by:
http://counterpunch.org/rahni04072007.html Test Tube Zealots: The American Chemical Society Terminates the Membership of Chemists from Iran By DAVID N. RAHNI The American Chemical Society (ACS) has once again led the way, with its "zealot" interpretation of "embargo" by the Department of Treasury's Office of Foreign Asset Control, by terminating the
16
1666
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it worked for strings. Thanks in advance, Damien
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.