Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# String Length

Tags:

c#

Why are those two string returning different values for length. "CertificateKey" is a property. It returns 41 for length. However, the equivalent constant string returns 40. If I copy the value of certificate as a constant, the length returns 41. Why?!?

// This is the property. Length 41
CertificateKey.Length

41

// This is a constant of the same string. Length 40
"9FE90CA8A4138F65E9E2C67D1F37B9D5B9919384".Length

40

// This is a copy of the value of the property above. Length 41
"‎9FE90CA8A4138F65E9E2C67D1F37B9D5B9919384".Length

41

like image 357
nul operator Avatar asked Dec 18 '25 13:12

nul operator


1 Answers

I copied and pasted your two string literals into LINQPad and found that I could reproduce your result, so then I printed each character like so:

var a = "9FE90CA8A4138F65E9E2C67D1F37B9D5B9919384";
var b = "‎9FE90CA8A4138F65E9E2C67D1F37B9D5B9919384";
foreach (char c in a) Console.WriteLine($"{(int)c:X}");
Console.WriteLine("---");
foreach (char c in b) Console.WriteLine($"{(int)c:X}");

And got the following result:

39
46
[...]
34
---
200E
39
46
[...]
34

200E is the left-to-right mark.

like image 186
Joe Farrell Avatar answered Dec 20 '25 01:12

Joe Farrell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!