比较两组字符串

引用内容 引用内容

【方法一】

StrComp(string1, String2, [Compare])

'函数功能:比较字符串string1和string2。
  
'返回说明:返回整数值:当string1<string2时,返回值<0; 当string1=string2时,返回值=0; 当string1>string2时,返回值>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



评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.