我试图比较两个字符串,但是,函数返回坏匹配。我厌倦了使用三种不同的方法并将每种方法的结果发布在不同的列中进行测试。我厌倦了使用“string1”=“string2”,instr()和strcomp()进行比较。
当使用相同的两个字符串时,它们甚至不会为每个方法返回相同的结果;有时一种比较是正确的,而另一些可能不是。
对于误报,Instr()返回一个位置,但是substring不在目标字符串中,Strcomp()返回0但是单词不相同,并且“string1”= string2“为true但字符串不同。
Worksheets("hemo").Cells(f, 1) = Trim(Worksheets(Stabv).Cells(Count, 1))
Worksheets("hemo").Cells(f, 2) = Len(Trim(Worksheets(Stabv).Cells(Count, 1)))
Worksheets("hemo").Cells(f, 3) = Trim(LitBaseArray(y))
Worksheets("hemo").Cells(f, 4) = Len(Trim(LitBaseArray(y)))
scrapeString1 = Trim(Worksheets(Stabv).Cells(Count, 1))
scrapeString2 = Trim(LitBaseArray(y))
If scrapeString1 = scrapeString2 Then
Worksheets("hemo").Cells(f, 5) = "true"
End If
If StrComp(Trim(scrapeString1), Trim(scrapeString2), vbTextCompare) = 0 Then
Worksheets("hemo").Cells(f, 6) = "true " & StrComp(Trim(scrapeString1), Trim(scrapeString2), vbTextCompare)
End If
If InStr(1, Trim(scrapeString1), Trim(scrapeString2), vbTextCompare) > 0 Then
Worksheets("hemo").Cells(f, 7) = "true " & InStr(1, Trim(scrapeString1), Trim(scrapeString2), vbTextCompare)
End If
f = f + 1
答案 0 :(得分:1)
1
If str1 = str2 Then
'str1 is exactly similar with str2
'"ABC" = "ABC", but "ABC" <> "Abc"
End If
2
If StrComp(str1, str2, vbTextCompare) = 0 Then
'str1 is similar with str2, but in case insensitive manner
'StrComp("ABC", "ABC", vbTextCompare) = 0 and
'StrComp("ABC", "Abc", vbTextCompare) = 0 as well
'Use binary comparison if you want similar behavior as for the = operator
End If
3
If InStr(1, str1, str2, vbTextCompare) > 0 Then
'Will be true if str1 contains str2 in a case insensitive manner
'To make it case-sensitive, use binary comparison instead of vbTextCompare
'InStr(1, Nothing, "A", vbTextCompare) will be 0
'InStr(1, "A", Nothing, vbTextCompare) will be 1 !
'InStr(1, "", "A", vbTextCompare) will be 1 !
'InStr(1, "ABC", "CD", vbTextCompare) will be 0
'InStr(1, "ABC", "BC", vbTextCompare) > 0
'InStr(1, "ABC", "AB", vbTextCompare) > 0
'InStr(1, "ABC", "abc", vbTextCompare) > 0
'InStr(1, "ABC", "c", vbTextCompare) > 0
End If
正如您所看到的,在不同的情况下可以预期不同的行为,特别是因为在某些情况下Trim
,而在其他情况下Trim
。