晕,网速太慢,打不开狼兄的链接.
建议朋友们以后直接贴上代码.
刚做了一个,测试一下:
Function MaxMatchString(ByVal Str1 As String, ByVal Str2 As String) As String
Application.Volatile
If Str1 = Str2 Then MaxMatchString = Str1: Exit Function
Dim Len_1 As Integer, Len_2 As Integer
Dim Str_Large As String, Str_Small As String, Str_Test As String
Len_1 = Len(Str1)
Len_2 = Len(Str2)
If Len_1 * Len_2 = 0 Then MaxMatchString = vbNullString: Exit Function
If Len_1 > Len_2 Then
Str_Large = Str1
Str_Small = Str2
Else
Str_Large = Str2
Str_Small = Str1
End If
For i = Len(Str_Small) To 1 Step -1
For j = 1 To Len(Str_Small) - i + 1
Str_Test = Mid(Str_Small, j, i)
If InStr(1, Str_Large, Str_Test) <> 0 Then MaxMatchString = Mid(Str_Small, j, i): Exit Function
Next
Next
End Function
Sub test()
Dim s1 As String, s2 As String
s1 = "我的家乡是哈尔滨"
s2 = "他的家乡也是哈尔滨"
MsgBox MaxMatchString(ByVal s1, ByVal s2)
End Sub