使用Visual Studio 2019和Visual Basic。下面的测试程序产生一个我无法解释的结果。使用InStr的前向搜索正确地找到了不区分大小写的人 CompareMethod.Text中的"joe“。但是反向搜索InStrRev没有找到"joe"...why?
Dim msg As String = "According to Joe, this is a beautiful day. Joe rides a bicycle!"
Dim joe As String = "Joe"
Dim joeLower As String = "joe"
Console.WriteLine(InStr(msg, joe)) ' 14 => found, expected
Console.WriteLine(InStr(msg, joeLower)) ' 0 => not found, expected
Console.WriteLine(InStr(msg, joeLower, CompareMethod.Text)) ' 14 => found, expected
Console.WriteLine(InStrRev(msg, joe)) ' 44 => found, expected
Console.WriteLine(InStrRev(msg, joeLower)) ' 0 => not found, expected
Console.WriteLine(InStrRev(msg, joeLower, CompareMethod.Text)) ' 0 => not found, why?发布于 2021-02-14 13:07:42
因为您作为第三个参数传递的是CompareMethod.Text值,而不是应该开始搜索的索引的值。默认值应为-1 (或从字符串的末尾)。
Console.WriteLine(InStrRev(msg, joeLower, -1, CompareMethod.Text)) ' => 44 InStrRev中的最后两个参数都是可选的,因为CompareMethod.Text等于整数1,所以搜索从索引1开始,只检查一个字符,当然没有找到匹配。
https://stackoverflow.com/questions/66195618
复制相似问题