Brady Moritz
Brady Moritz

Reputation: 8903

String equality with null handling

I will often use this code to compare a string:

if(!string.IsNullOrEmpty(str1) && str1.Equals(str2)){
    //they are equal, do my thing
}

This handles the null case first etc.

Is there a cleaner way to do string comparison, perhaps with a single method call that will handle possible null values? I simply want to know that the strings are not equal if the testing value is null.

(I'm having dejavu that I may have asked this before, I apologize if so)


Update: In my case, the str2 is a known good string to compare to, so I don't need to check it for null. str1 is the "unknown" string which may be null, so I want to say "str1 does not equal str2" in the cases where str1 is null...

Upvotes: 50

Views: 77023

Answers (8)

SLaks
SLaks

Reputation: 887225

Unlike Java, C# strings override the == operator:

if (str1 == str2)

If you want a case-insensitive comparison:

if (string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase))

Upvotes: 60

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

If you do not want to treat two null strings as equal to each other, your code is optimal.

If, on the other hand, you want to treat null values as equal to each other, you can use

object.Equals(str1, str2)

for a more "symmetric" approach that also handles null values.

This is not the only approach to a symmetric check of string equality that treats null strings as equal: you could also use string.Equals(str1, str2) or even str1 == str2, which redirects to string.Equals.

Upvotes: 24

Brady Moritz
Brady Moritz

Reputation: 8903

In more recent C# versions, we also have the null coalescing option now. This isn't an exact solution but can be a cleaner one for some purposes. It takes some liberties with "upgrading" str1 to an empty string for comparison purposes, in the instances where it actually is a null.

if((str1??"").Equals(str2))
{
    //they are equal, do things here
}

Upvotes: 1

Juan Medina
Juan Medina

Reputation: 559

I know this has been answer while ago and there's good answers, although as per Microsoft documentation (https://learn.microsoft.com/en-us/dotnet/api/system.string.equals?view=net-5.0) you can use the following to check both cases:

if(string.Equals(str1,str2)){
    //they are equal with null handling
}

Upvotes: 1

Emad Jazer
Emad Jazer

Reputation: 31

This will do it:

string.IsNullOrWhiteSpace(str1) ? string.IsNullOrWhiteSpace(str2) : str1.Equals(str2, StringComparison.OrdinalIgnoreCase);

Upvotes: 3

dotsven
dotsven

Reputation: 326

I know this is some years old and I think the solution from dasblinkenlight is functionally perfect for what you asked for. However I do prefer this code for readability reasons:

String.Equals(str1, str2)

Upvotes: 19

Justin Pihony
Justin Pihony

Reputation: 67065

There is no built in way to do this, but you could create an extension method to encapsulate this.

public static StringExtensions
{
    public static Boolean IsNotNullAndEquals(this string str1, string str2)
    {
        return !string.IsNullOrEmpty(str1) && str1.Equals(str2)
    }
}

then use it like this:

str1.IsNotNullAndEquals(str2);

Naming is going to be your hardest thing here IMO...since you need to convey that you are only null checking str1. When used as an extension method, it reads fairly well, but if used as a regular static, then it doesn't convey that as well.

Upvotes: 6

user1968030
user1968030

Reputation:

You can use this code

object.Equals(str1, str2)

Upvotes: 1

Related Questions