Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Equality - What is going on here? [closed]

Tags:

string

c#

In order to debug an issue in my code, I have declared the following two strings, assuming they would be equivalent:

String print = "8A9B485ECDC56B6E0FD023D6994A57EEC49B0717";
String newPrint = thumbprint.Trim().Replace(" ", "").ToUpper();

I discovered they are not. Great, this is the source of my issue. However, I'm checking things in the immediate window (on the line following the declarations) and don't understand what is happening. Here is the output:

print
"8A9B485ECDC56B6E0FD023D6994A57EEC49B0717"
newPrint
"‎8A9B485ECDC56B6E0FD023D6994A57EEC49B0717"
String.Compare(print, newPrint);
0
print == newPrint
false
print.Equals(newPrint)
false

huh? Why aren't they equal?

edit:

I need to use 'thumbprint' as the base. It's a user entered string. I'm just using 'newPrint' as a temporary variable to hold the trimmed/uppered value. print is the expected outcome.

like image 383
Erix Avatar asked Sep 13 '12 13:09

Erix


People also ask

Why == does not work with string?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Is vs == for strings?

The == and is Operators At first sight they seem to be the same, but actually they are not. == compares two variables based on their actual value. In contrast, the is operator compares two variables based on the object id and returns True if the two variables refer to the same object.

Can we use == for string?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

What is the correct way of checking string equality?

You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.


2 Answers

Indeed, they are not equivalent. I copied the two values and newPrint has a length of 41 whereas print has a length of 40. The first character of newPrint is a character with the ASCII value 14. Interesstingly, this has been transfered from your immediate window to SO to my LINQPad.

print.Length
40
newPrint.Length
41
(sbyte)print[0]
56
(sbyte)newPrint[0]
14

That actually has nothing to do with your Trim and Replace calls but with the fact that you are using thumbprint instead of print as the base. I can only assume that thumbprint contains that additional character. Where it comes from I don't know. If you would change your second line to use print instead of thumbprint you would get the result you expect.

like image 122
Daniel Hilgarth Avatar answered Nov 15 '22 14:11

Daniel Hilgarth


The strings that you're posted are not equal. Just do this:

string val   = "8A9B485ECDC56B6E0FD023D6994A57EEC49B0717"; 
string val1  = "‎8A9B485ECDC56B6E0FD023D6994A57EEC49B0717";
var bt = System.Text.Encoding.UTF8.GetBytes(val);
var bt_1 = System.Text.Encoding.UTF8.GetBytes(val1);

You will see that the second array contains more element in begining.

226 
128 
142

after this 3 elements content is equal.

The reason String.Compare works is cause:

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters

like image 33
Tigran Avatar answered Nov 15 '22 16:11

Tigran