Beginner's Guide For Comparing Strings in C#
May 22, 2023
• 1,835 views
This video is a brief high-level overview of several different ways that you can compare strings in C#. While more advanced users may know of alternatives, this video is intended for beginners to get a better feel for a few of the most common ways that you can check if two strings are equivalent.
For more videos on programming with detailed examples, check this out:
https://www.youtube.com/playlist?list=PLzATctVhnsghN6XlmOvRzwh4JSpkRkj2T
Check out more Dev Leader content (including full in-dep...
View Transcript
so this video will just serve as an introductory look at string comparisons in C sharp so if you're a beginner programmer and you're starting to write some applications where you're trying to compare some strings maybe you're looking up some examples online and seeing that there's a handful of different ways that you can do these comparisons and you're getting a little bit confused this video will just touch on some of the basics for that and why you might want to use different variations of it so let's jump right over to visual studio and get started alright so here in Visual Studio I just have two strings declared my string one and my string two I have Hello assigned to the first string and World assigned to the second string so we as programmers know that these are obviously two different words and these strings should not
be equal but if we wanted to compare them and actually see if they were equal and get a Boolean value how would we go about doing that and I think probably the most obvious way that people jump to is that they want to do a comparison like this with a double equal sign and this is going to be the first example that we look at here so let's go ahead and print this to the console and when we go to run this of course as the comment suggests and as we know that these two things will not be equal all right and when we run this as expected we get a false printed here so everything is all and well but there's other ways that we could go do this string comparison so I'm going to go ahead and paste two more examples here and
this the second one that we see see here is just comparing my string one equals my string two and of course like all the comments say we're not changing these strings at all so yes these will all continue to be false but this is another way that you could go ahead and do this and the other way is using this string dot compare method and you'll notice that this looks a little bit more bizarre compared to just calling equals or doing this equality operator so we're going to call this method compare pass in string one and string two and you'll notice that at the end it's comparing it to zero so let's talk about the couple of variations that we have on the screen and I'm not going to go ahead and run this and print things to the console just because at this point
it's not going to be super helpful and let's go ahead and maybe reduce this so I can zoom in a little bit more and make that bigger for everyone to see this first example uses the equality operator which is double equal sign and if we compare that here to the equals method on string what's the difference between these two things well this equals method actually allows us to pass in a comparison type and the C sharp or the Microsoft guidelines actually suggest that we do not want to be using equality comparison methods any of the overloads that do not specify a string comparison so it is important to note that this here just by what I was saying implicitly tells us how we're comparing these two things so you as a beginner programmer might not know if these two things are compared there's a handful
of different variations so there's ordinal there's current culture there's invariant culture then you have case sensitivity so if we make this a little bit more interesting and I put a capital o on the end of this maybe that's a bad one just because O's look the same almost Capital H looks different and capital E right are these two things equal well if you look at this example on line five you as a beginner programmer might go well maybe they are equal because they're the same word they're just different casing and if you ran this and it was false you might go oh actually maybe I'm not surprised like it's technically not equal but what if you did want them to be equal what if you were trying to look at some user input and trying to check and you don't care if the casing's off
because maybe you're going to go change it later for them and you just want to do a comparison well this equals overload that takes In the comparison type can actually help you a lot here so we can do string comparison and you'll notice maybe it's kind of hard to see because the text is small but we have string comparison there's ordinal ordinal ignore case you have invariant culture ignore case current culture ignore case current culture there's all sorts of comparison methods here and what we might want to look at especially for performance reasons is ordinal and then ignore case in this example so just again to fit it all on the screen for us it would look something like this and this here would actually give us the ability to do a case insensitive string comparison and that would mean that my string 1 and
my string 2 are in fact equal I'm going to come back to this in just a second because there's a bit of a catch here by using this syntax and I'll show us how to fix that right at the end as well if we jump to the third example and we use this compare method the reason that this compare method exists and actually gives you an integer return type is because they can use this for sorting so if you wanted to know if hello came before hello with a capital E and Capital H then when you go to run this comparison this result that you'll get coming out of here will actually tell you which one comes before and which one comes after or if it's equal to zero then the comparison essentially is saying that these two strings are equal based on the comparison
type however just like the example before and the guidelines that we have we are not supposed to use overloads that don't have any string comparison put inside of them so we do have access again to string comparison here we could do ordinal ignore case again and this would come out to being true now so to quickly recap before I explain the little catch on this one here the three variations we have are this for first one with a double equality sign and this is actually something I would suggest that you do not use and again this is because based on the coding guidelines that we have from Microsoft we do not want to be using string comparisons with an implicit comparison type now when I say implicit it's because it's not actually explicitly written on screen here you might know that a double equal sign
actually does mean an ordinal comparison that is case sensitive but it's not written here explicitly the second one that we have here is probably the most common way that I would say that people end up doing string comparisons and getting their string comparison type included as well so this is probably the most common way it does have a catch and I'm going to come back to it in just a moment and then the third example we have is usually what you would do if you were trying to sort or order strings so if you're trying to check if they're equal I mean this will absolutely work it will function the exact same way that this second example works it's just a little bit more verbose and if you don't care about the actual ordering then this is maybe a little bit Overkill to use okay
if we go back to the second example there is a bit of a catch here and I just wanted to note it so you'll notice that my string one we're calling the method equals on my string one if for some reason we had a null for my string one see how the squiggly line just came up from Visual Studio it's indicating that this is actually going to be null if we were to call this this is actually going to throw a null reference exception so I would actually change example number two let's comment it out I also copied it I'm just going to paste it directly below what I would actually recommend doing is this you put string and we're going to call the equals operator right on the class string and that's just going to be a static method we call and we'll compare
this way now when we call this part if we're replacing example two with this we actually can avoid null reference exceptions and in my opinion this is probably one of the safest ways that you can do your comparisons in a follow-up video I'll end up showing us the difference in performance characteristics of doing string comparisons with ordinal or using invariant culture so we'll come back to that in a future video but for now I would recommend that you're probably going to want to use ordinal or ordinal ignored case all right so that was just a super basic look at three different ways that we can compare strings in C sharp and I just wanted to point out that if you are a more advanced user watching this you might be rolling your eyes saying well Nick there's all these other things we should be talking
about for string comparisons there's all these performance implications or there's we could be looking at spans and doing other types of things for sure we can absolutely do that but this is just a super quick look for beginners and hopefully this serves as a way that you can start building applications and doing better string comparisons than just using a double equal sign so thank you for watching right to the end if you're interested in working with strings then this video right here is going to be really helpful for you for your next steps
Frequently Asked Questions
What are the different ways to compare strings in C#?
In this video, I discussed three main ways to compare strings in C#. The first is using the equality operator (==), which I don't recommend due to implicit comparison types. The second is using the Equals method, which allows for specifying a comparison type, making it safer and more flexible. The third method is the String.Compare method, which is useful for sorting strings and also allows for comparison types.
Why should I avoid using the equality operator for string comparisons?
I suggest avoiding the equality operator because it uses an implicit comparison type, which can lead to confusion and potential errors. It's not explicitly clear what type of comparison is being performed, which is why I recommend using the Equals method instead, as it allows you to specify the comparison type explicitly.
How can I prevent null reference exceptions when comparing strings?
To avoid null reference exceptions, I recommend calling the Equals method directly on the String class instead of on an instance of a string. This way, if the string is null, it won't throw an exception, making your comparisons safer.
These FAQs were generated by AI from the video transcript.