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
Solved

Compairing two strings the "right way"

Posted on 2013-01-19
8
155 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: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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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 …

837 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