---
title: "VB 比较两组字符串 "
author: "yeuego"
date: "2011-03-09T13:18:28Z"
tags: ["VB"]
categories: ["研发管理"]
slug: "vb"
draft: false
lastmod: "2011-03-09T13:18:28Z"
id: 948237
url: "https://yeuego.iteye.com/blog/948237"
---

【方法一】

> StrComp(string1, String2, \[Compare])
> 
> '函数功能：比较字符串string1和string2。
> 
> '返回说明：返回整数值：当string1string2时，返回值&gt;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 &lt;&gt; Len2 Then '长度不等直接退出  
> If Instr(str1, str2) &gt; 0 Or Instr(str2, str1) &gt; 0 Then  
> MyStrCmp = "长度不同，有匹配字符存在。"  
> Else  
> MyStrCmp = "完全不同。"  
> End If  
> Exit Function  
> ElseIf Len1 = Len2 Then  
> For TmpFor = 1 To Len1  
> If Mid(str1, TmpFor, 1) &lt;&gt; 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