Solved

Compairing two strings the "right way"

Posted on 2013-01-19
8
161 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:Michael Fowler
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

623 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