简体   繁体   中英

C# Two strings, visually the same, yet they are not Equal nor Equivalent

I have a strange situation I can't figure out.

I am using a third party conversion framework which is expecting units in abbreviated form eg "μV" which is MicroVolts

But when I go to parse the string "μV" as MicroVolts it fails.

I boiled it down to the fact the abbreviation string I pass in is not equal to the string the third party framework is using for Microvolts, even though they look identical.

Here is the output of the Immediate window, to help shed some light on the context:

targetUom
"µV"
targetUom.GetHashCode()
-837503221
"μV".GetHashCode()
-837502956
targetUom.Equals("µV") // This is using the value of targetUom
true
targetUom.Equals("μV") // This is using the value from the 3rd party framework
false

I have obtained the value used in the third party framework by debugging and copying the value of the abbreviation I know they use for MicroVolts.

Any idea why two strings, even though the look to be made up of the exact same characters, would not be considered equal?

I've also compared the first character, the micro unit representation, between the two strings which yields:

'μ'.CompareTo(targetUom[0])
775

*********** UPDATE **************** So I've found that the two micro characters are different encodings.

But when i attempt to use the same encoding that the target framework uses, Visual Studio gives me this message:

在此处输入图片说明

What are the implication of changing the encoding of the file..should I be doing this or should i collaborate with the framework author to enable their framework to handle both encodings?

Turns out there are two unicode characters which are probably identical in most fonts:

  1. Greek small letter mu, U+03BC
  2. Micro sign, U+00B5

You can access them both in strings using the \\u escape:

Console.WriteLine("Greek small letter mu: \u03bc");
Console.WriteLine("Micro sign: \u00b5");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM