I am baffled what is going on here:
Dim sSearchIn As String = "Bla"
Dim sSearchFor As String = Vb6ChrW(65533) 'This results in the string "�"
Dim i As Integer = sSearchIn.IndexOf(sSearchFor, 0)
I expect "i" to be -1 because the string "�" does not exist in the string "Bla".
However, "i" is 0.
What am I missing?
Thank you!
Public Function Vb6ChrW(ByVal uKeyCode As Integer) As String
Dim nChar As Char = Char.ConvertFromUtf32(uKeyCode)
Return New String(nChar, 1)
End Function
CodePudding user response:
You are performing culture-sensitive search. It is behaving as documented:
In a culture-sensitive search, if
value
contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, theIndexOf(String, Int32)
method always returnsstartIndex
If you wanted a char-code-level search, you should have used
sSearchIn.IndexOf(sSearchFor, StringComparison.Ordinal)
, that gives -1 as you expect.
You should also turn Option Strict On
so that it correctly brakes your Vb6ChrW
function that you don't need in the first place because ChrW
exists.