Solved

Compairing two strings the "right way"

Posted on 2013-01-19
8
158 Views
Last Modified: 2013-02-18
Hi Experts,

      We have a debate going on in the office!  I am looking for takes on what is the industry accepted "standard" or elegant way to accomplish this:

string1.Trim().ToUpper() == string2.Trim().ToUpper()

There is one million ways of doing it, we all agree on using string.Equals, but where to from there?  This is more like a survey than a question...

Thanks!
0
Comment
Question by:axnst2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38797161
I do this all the time but it is not always the best. Check http://www.blackwasp.co.uk/StringComparison.aspx
0
 
LVL 23

Expert Comment

by:Michael74
ID: 38797835
You could always use

Compare(String, String, Boolean)

http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx

For small strings I would assume their is no appreciable difference between the methods

Michael
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38797892
Actually I would say that could well be the completely wrong way.  It will give false positives.
If I compare the two different strings "abc" and "Abc" that will return they are the same but they are not the same.  (Some programming languages and operating systems are case sensitive - using that in a source comparison program will NOT help you find where something was mistyped.)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 38797894
"right way" for what purpose?
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38797919
The best available way for string comparison is String.Compare(string, string, bool), because if you are calling string1.Trim().ToUpper() == string2.Trim().ToUpper()

first of all you need to call ToUpper() for two strings and then "==" operator will again call Equals() method of string class which in turns calls String.Compare(string, string, CultureInfo, CompareOptions) look at internal implementation of == operator :

public static bool operator ==(string a, string b)
		{
			return string.Equals(a, b);
		}

Open in new window


Implementation of Equals() method

 public static bool Equals(string a, string b)
{
	return a == b || (a != null && b != null && a.Length == b.Length && string.EqualsHelper(a, b));
}

Open in new window



public static int Compare(string strA, string strB, CultureInfo culture, CompareOptions options)
{
	if (culture == null)
	{
		throw new ArgumentNullException("culture");
	}
	return culture.CompareInfo.Compare(strA, strB, options);
}

Open in new window



some more explanation:

http://www.willasrari.com/blog/stringcompare-versus-stringequals/000189.aspx

http://blogs.msdn.com/b/bclteam/archive/2007/05/31/string-compare-string-equals-josh-free.aspx
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 total points
ID: 38797922
some correction the best method is
String.Compare(string, string, CultureInfo, CompareOptions)
0
 
LVL 40
ID: 38798701
There is no generic best way. It depends on what results you expect (case sensitive, accent sensitive) and the language(s) that the application can encounter.

String.Compare is usually the best tool because it handles most of the job for you and can adapt to any situation. There are 10 overloads to that method, and each one can be the best, depending on the need.
0
 

Author Closing Comment

by:axnst2
ID: 38903130
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How we can add Dten600.dll in GAC in c#. 18 55
Adjust the codes 3 63
Shared Service Environment 2 54
.NET Redemption/Outlook Examples 3 34
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is about my first experience with programming Arduino.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question