字符串比较问题

jp0077777 2009-08-01 05:00:38
有字符串1 "9 1 10 6 13"
另一字符串2 "9 10 11 12 13"

查找字符串1中是否 有任意3个数字 在字符串2中 例子中 9 10 13为这3个数
如果有则 则显示出"有3个数相同" 并且显示出字符串2中另外2个数字 例子中为11 12

有好的方法编程得到吗?
谢了
...全文
109 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
东方之珠 2009-08-01
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()

Dim Str1 As String, Str2 As String
Dim S1() As String, S2() As String, S3() As String
Dim S As String
Dim i As Integer, j As Integer, m As Integer, n As Integer

Str1 = "9 1 10 6 13"
Str2 = "9 10 11 12 13"

S1 = Split(Str1, " "): S2 = Split(Str2, " ")

m = 0: n = 0

For i = 0 To UBound(S1)
For j = 0 To UBound(S2)
If Val(S1(i)) = Val(S2(j)) Then
m = m + 1: S = S & (S1(i) & " ")
Debug.Print "相同的数是:" & S1(i)
End If
Next
Next

n = UBound(S2) + 1 - m

S3 = Split(S, " ")

For i = 0 To UBound(S2)
For j = 0 To UBound(S3)
If Val(S2(i)) = Val(S3(j)) Then
S2(i) = ""
End If
Next
If Len(S2(i)) <> 0 Then Debug.Print "不相同的数是:" & S2(i)
Next

Debug.Print "共有" & m & "个相同的数!", "共有" & n & "个不相同的数!"

End Sub
jp0077777 2009-08-01
  • 打赏
  • 举报
回复
谢了
SYSSZ 2009-08-01
  • 打赏
  • 举报
回复
Private Sub Form_Load()
S1 = "9 1 10 6 13"
S2 = "9 10 11 12 13"
A = Split(S1, " ")
B = Split(S2, " ")
For i = 0 To UBound(A)
For J = 0 To UBound(A)
If A(i) = B(J) Then
k = k + 1
Debug.Print A(i)
End If
Next
Next
Debug.Print "有" & k & "个数相同"
End Sub
jp0077777 2009-08-01
  • 打赏
  • 举报
回复
请用VB好吗? 不然 我怎么加到我的程序中
sonic_andy 2009-08-01
  • 打赏
  • 举报
回复
a = "9 1 10 6 13" 
b = "9 10 11 12 13"

arr_a = split(a," ")
arr_b = split(b," ")

for i=lbound(arr_a) to ubound(arr_a)
For j=LBound(arr_b) To UBound(arr_b)
If arr_a(i) = arr_b(j) Then
Call MsgBox(arr_a(i))
End If
Next
Next


这样也可以:->
sonic_andy 2009-08-01
  • 打赏
  • 举报
回复
a = "9 1 10 6 13" 
b = "9 10 11 12 13"

arr_a = split(a," ")
arr_b = split(b," ")

set dict = createobject("scripting.dictionary")
for i=lbound(arr_b) to ubound(arr_b)
call dict.add(arr_b(i),null)
next

for i=lbound(arr_a) to ubound(arr_a)
if dict.exists(arr_a(i)) then
call msgbox(arr_a(i))
end if
next


将以上代码复制到记事本中,保存为test.vbs,然后双击运行即可.
goosen 2009-08-01
  • 打赏
  • 举报
回复
帮顶
chinaboyzyq 2009-08-01
  • 打赏
  • 举报
回复
还有我看到你的贴子,可能还有简单的方法,不过我这个可以实现你的要求.
chinaboyzyq 2009-08-01
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim tempStr1() As String, tempStr2() As String
Dim Str1 As String, Str2 As String, temp As String
Dim i As Integer, j As Integer, N As Integer, F() As Boolean

Str1 = "9 1 10 6 13"
Str2 = "9 10 11 12 13"

tempStr1 = Split(Str1)
tempStr2 = Split(Str2)
ReDim F(UBound(tempStr2))

For i = 0 To UBound(tempStr1)
For j = 0 To UBound(tempStr2)
If tempStr1(i) = tempStr2(j) Then
F(j) = True
N = N + 1
End If
Next j
Next i

If N = 3 Then
For i = 0 To UBound(tempStr2)
If Not F(i) Then temp = temp + tempStr2(i) + " "
Next i

MsgBox "有3个数据相同,不同数为:" & temp
End If
zss427607 2009-08-01
  • 打赏
  • 举报
回复
路过 看看
jp0077777 2009-08-01
  • 打赏
  • 举报
回复
人呢?
jp0077777 2009-08-01
  • 打赏
  • 举报
回复
?今天都高手休息了

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧