Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Compairing two strings the "right way"

Posted on 2013-01-19
8
Medium Priority
?
169 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 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 45

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 85

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

963 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