VB 比较两组字符串

【方法一】

StrComp(string1, String2, [Compare])

'函数功能:比较字符串string1和string2。

'返回说明:返回整数值:当string1string2时,返回值>0。这种比较是按照字符的字典序进行比较。


【方法二】

Declare Function lstrcmp Lib "kernel32" Alias "lstrcmpA" ( ByVal lpString1 As String , ByVal lpString2 As String ) As Long

'或者用API的lstrcmp也能很快地实现字符串的对比


【方法三】

'By JiaJia 2008-05-30
Private Function MyStrCmp(str1 As String , str2 As String ) As String
Dim
TmpFor As Long , Log As Long
Dim
Len1 As Long , Len2 As Long
If
Len1 <> Len2 Then '长度不等直接退出
If Instr(str1, str2) > 0 Or Instr(str2, str1) > 0 Then
MyStrCmp = "长度不同,有匹配字符存在。"
Else
MyStrCmp = "完全不同。"
End If
Exit Function
ElseIf
Len1 = Len2 Then
For
TmpFor = 1 To Len1
If Mid(str1, TmpFor, 1 ) <> Mid(str2, TmpFor, 1 ) Then Log = Log + 1
Next
If
Log = 0 Then
MyStrCmp = "长度相同,有 " & Log & " 个字符不同。"
Else
MyStrCmp = "完全相同。"
End If
Exit Function
End If
End Function

猜你喜欢

转载自yeuego.iteye.com/blog/948237
VB
今日推荐