We have recently upgraded all our projects from .NET 3.5 to .NET 4. I have come across a rather strange issue with respect to string.IndexOf()
.
My code obviously does something slightly different, but in the process of investigating the issue, I found that calling IndexOf()
on a string with itself returned 1 instead of 0.
In other words:
string text = "\xAD\x2D"; // problem happens with "-dely N.China", too;
int index = text.IndexOf(text); // see update note below.
Gave me an index of 1, instead of 0. A couple of things to note about this problem:
The problems seems related to these hyphens (the first character is the Unicode soft hyphen, the second is a regular hyphen).
I have double checked, this does not happen in .NET 3.5 but does in .NET 4.
Changing the
IndexOf()
to do an ordinal compare fixes the issue, so for some reason that first character is ignored with the defaultIndexOf
.
Does anyone know why this happens?
EDIT
Sorry guys, made a bit of a stuff up on the original post and got the hidden dash in there twice. I have updated the string, this should return index of 1 instead of 2, as long as you paste it in the correct editor.
Update:
Changed the original problem string to one where every actual character is clearly visible (using escaping). This simplifies the question a bit.
0
... – Nahshu