Solved

Compairing two strings the "right way"

Posted on 2013-01-19
8
150 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
8 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
"right way" for what purpose?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:Naman Goel
Comment Utility
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
Comment Utility
some correction the best method is
String.Compare(string, string, CultureInfo, CompareOptions)
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now