Click here to Skip to main content
13,559,182 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


35 bookmarked
Posted 24 Aug 2012
Licenced CPOL

A Beginner's Tutorial on String Comparison in C#

, 24 Aug 2012
Rate this:
Please Sign up or sign in to vote.
This small Tip talks about the right way of comparing strings in a C# application.


This small article talks about the right way of comparing strings in a C# application. We will try to see what are the various ways we can compare the strings and which one should be or should not be used.


Usually in our applications when we want to compare two strings we use the equality operator. Under most scenarios this will work properly but still we should know what are the other ways we can do string comparisons and perhaps achieve better performance and results. So lets say I have a variable str and I want to check whether its value is equal to "Yes" or not.

if(str == "Yes")
	// equal
	// not equal

The above mentioned operator will do the comparison in a case sensitive manner and it will not consider the current culture. Now if a non case sensitive comparison is required I have seen most of the developers taking either of the below mentioned approaches.

Either we do this

if (str.ToLower() == "yes")
	//not equal

or we do something like

if (str.ToUpper() == "YES")
	//not equal

Now this will work fine in most cases and since the immutable nature of the string will not even modify my original string, it does involve an extra function call and creation of an extra temporary string variable(call to ToLower() or ToUpper()). And it will not work in case we have this code running in a culture sensitive application and the str variable might contain some characters that are non-English characters.

So how do we do string comparison in a way that circumvent all these problems. .Net framework string class already takes care of all these scenarios and provide us some functions that will enable us to perform correct and optimal string comparison in all such scenarios. We will now look into these functions.

Note: We will talk about equality comparison but all these points will be valid for other comparisons too i.e. finding the order of strings etc.

Using the Code  

The very first thing to understand before jumping on the functions is the type of comparisons I might need. I might need a culture sensitive comparison or a non culture sensitive comparison(Ordinal comparison). secondly, I might want a case sensitive information or case insensitive comparison.

Now let us look at what .Net provides us. .Net provides us 3 Modes.

  1. CultureInvariant
  2. CurrentCulture
  3. Ordinal 


The CultureInvariant mode assumes that all the comparisons will be done in English language and en-US as the culture. This mode interpret characters with reference to a particular alphabet. The alphabets are ordered assuming the en-US as the culture. This mode ultimately can be visualized as using this sort of string to find the order of string: "AaBbCc...". So in this mode the sting "CAT" and "bat" will be ordered as: "bat", "CAT".


The 2nd mode CurrentCulture, this will arrange the alphabets as arranged in case of Invariant culture to find the order of strings, only this order will be culture specific.

Also in this mode, The characters are compared using their corresponding counterpart in the other culture i.e the German Ä will be treated as A of en-US. 


The 3rd mode Ordinal simply compares the strings based on the order of characters. In other words, it simply uses the Unicode value of the characters to find the order. It uses following reference string for ordering strings. Which is nothing but all alphabets ordered as per their Unicode/ASCII values: "". So in this mode the sting "CAT" and "bat" will be ordered as: "CAT", "bat". 

Now with this information at hand let us see what .Net provides us. The String.Equals and compare functions have an overloaded version which takes StringComparison enum type as the argument. This argument will specify the mode which we want to use for this comparison. 

public static bool Equals (string a, string b, StringComparison comparisonType);

This enum could have these possible values

  • CurrentCulture
  • CurrentCultureIgnoreCase
  • InvariantCulture
  • InvariantCultureIgnoreCase
  • Ordinal
  • OrdinalIgnoreCase 

Looking at each enum value it is self explanatory which mode is for which scenario. Still Let us draw a small matrix for the same.

CaseSensitive Non Casesensitive  
Culture Sensitive CurrentCulture CurrentCultureIgnoreCase
Non culture sentitive(English en-US) InvariantCulture InvariantCultureIgnoreCase
Order Ordinal OrdinalIgnoreCase

And now I do the same comparison which we saw above using these modes.

Comparing the string character to character in a case sensitive manner.

if (String.Equals(str, "Yes", StringComparison.Ordinal) == true)
	//not equal

Comparing the string in a non case sensitive manner.

if (String.Equals(str, "Yes", StringComparison.OrdinalIgnoreCase) == true)
	//not equal

These code snippets will also give us the desired results and perhaps in a little efficient way than the earlier.

Note: The == operator is equals to <code>StringComparison.Ordinal. So in cases we need to use this mode we can simply do away with the == operator.

Now let us summarize and see which one should be used when

  • CurrentCulture - Culture specific case sensitive comparison.
  • CurrentCultureIgnoreCase - Culture specific case non-sensitive comparison.
  • InvariantCulture - English only case sensitive comparison.
  • InvariantCultureIgnoreCase - English only non-case sensitive comparison.
  • Ordinal - ASCII/UNICODE value based case sensitive comparison.
  • OrdinalIgnoreCase - ASCII/UNICODE value based non-case sensitive comparison.

A Note on StringComparer and StringComparison

A very interesting point of confusion is the possibility of being able to user StringComparer class for all the similar string comparisons. This class also has all these 6 ways doing the string comparisons. Important thing to note here is that this Class also implements comparison interfaces i.e. IComparer, IEqualityComparer, IComparer<String>.

The StringComparison that we have discussed so far in this Tip is an enum that you we should use while comparing 2 strings. So when should we not use this above mentioned approach and go for the StringComparer class.

The thumb rule is that if only string comparison is needed then we should use String class's methods like String.Equals Which will use the StringComparison enum to determine which mode should be used for actual comparison. You Should use StringComparer class only when we have some  methods which take any one of IComparer, IEqualityComparer, IComparer<String> type as a parameters and we need to pass our strings.

Perhaps, internally the String class's methods are still using StringComparer class for actual comparison but from a developer's perspective following the above guideline should suffice.

Point of Interest

This small article is written for those developers who are still in the start of their career and they are manipulating string in various forms just to achieve the desired comparison results. We have discussed only the equality operation but comparison operator will also follow the same rules.


  • 24 August 2012: First version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rahul Rajat Singh
Software Developer (Senior)
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more:[^]

  • Microsoft MVP 2015

You may also be interested in...

Comments and Discussions

GeneralThoughts Pin
PIEBALDconsult9-Nov-13 9:35
professionalPIEBALDconsult9-Nov-13 9:35 
GeneralMy vote of 5 Pin
ThatsAlok4-Jun-13 0:39
memberThatsAlok4-Jun-13 0:39 
GeneralMy vote of 5 Pin
wangxj_nemo16-Jan-13 22:49
memberwangxj_nemo16-Jan-13 22:49 
SuggestionC# Guidelines Pin
StevenFoust3-Jan-13 5:47
memberStevenFoust3-Jan-13 5:47 
GeneralMy vote of 5 Pin
Christian Amado24-Aug-12 16:45
memberChristian Amado24-Aug-12 16:45 
GeneralMy vote of 4 Pin
Vitaly Tomilov24-Aug-12 6:56
memberVitaly Tomilov24-Aug-12 6:56 

The one people use most is as shown below, which you did not even mention...

public static int Compare(string strA, string strB, bool ignoreCase);

GeneralMy vote of 4 Pin
Chip VonBurg24-Aug-12 5:22
memberChip VonBurg24-Aug-12 5:22 
GeneralMy vote of 3 Pin
Andreas Gieriet24-Aug-12 1:40
memberAndreas Gieriet24-Aug-12 1:40 
AnswerRe: My vote of 3 Pin
Rahul Rajat Singh24-Aug-12 1:44
memberRahul Rajat Singh24-Aug-12 1:44 
GeneralRe: My vote of 3 Pin
Andreas Gieriet24-Aug-12 2:00
memberAndreas Gieriet24-Aug-12 2:00 
AnswerRe: My vote of 3 Pin
Rahul Rajat Singh24-Aug-12 2:05
memberRahul Rajat Singh24-Aug-12 2:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 24 Aug 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid