用VB实现一个简单的字符串比较函数,谢谢。

hoyandchie 2010-07-08 11:03:41
问题描述:

现在两个字符串str1,str2。这两个字符串都是用“,”连接起来的。函数实现功能如下:

用“,”分别将两个字符串拆开,只要它们分别组成的集合相等,就输出1,否则输出0。

比如: str1=A,C,D str2=C,D,A 则要求返回1
str1=A,C,D str1=C,D 则要求返回0


估计这个函数不难实现,可是我刚接触VB,还是有点困难,请高手帮忙,不胜感激。
...全文
218 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcrun 2010-07-08
  • 打赏
  • 举报
回复
大家回答的不错,建议继续从vb.net的角度思考一下:)
hoyandchie 2010-07-08
  • 打赏
  • 举报
回复
非常感谢楼上的几位朋友给予精彩的答复!谢谢!
king06 2010-07-08
  • 打赏
  • 举报
回复
利用Combo1的排序功能自动排序再比较就方便了
'Combo1和Combo2的Sorted属性都设置为True
Private Sub Command1_Click()
Dim str1 As String: str1 = "A,C,D"
Dim str2 As String: str2 = "C,D,A"
Dim arr1() As String: arr1 = Split(str1, ",")
Dim arr2() As String: arr2 = Split(str2, ",")
Dim i As Long, str11 As String, str22 As String
If UBound(arr1) <> UBound(arr2) Then
MsgBox "0"
Exit Sub
Else
For i = 0 To UBound(arr1)
Combo1.AddItem arr1(i)
Combo2.AddItem arr2(i)
Next
For i = 0 To Combo1.ListCount - 1
str11 = str11 & "," & Combo1.List(i)
str22 = str22 & "," & Combo2.List(i)
Next
If str11 = str22 Then
MsgBox "1"
Else
MsgBox "0"
End If
End If
End Sub
CHRL 2010-07-08
  • 打赏
  • 举报
回复
Option Explicit



Public Function compStr(ByVal vStr1 As String, ByVal vStr2 As String) As Integer
Dim vArr1() As String
Dim vArr2() As String
Dim i As Integer
Dim j As Integer
Dim IsComp As Boolean
Let vArr1 = Split(vStr1, ",")
Let vArr2 = Split(vStr2, ",")

If UBound(vArr1) <> UBound(vArr2) Then
compStr = 0
Exit Function
End If

For i = 0 To UBound(vArr1)
IsComp = False
For j = 0 To UBound(vArr2)
If vArr2(j) = vArr1(i) Then
IsComp = True
compStr = 1
Exit For
End If
Next
If Not IsComp Then
compStr = 0
Exit For
End If
Next

End Function

Private Sub Command1_Click()
Dim b As Integer
b = compStr(Text1.Text, Text2.Text)
MsgBox b
End Sub
倒大霉的上帝 2010-07-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jhone99 的回复:]
VB code

Private Sub Command1_Click()
Dim str1 As String
Dim str2 As String


str1 = "A,C,D,C"
str2 = "A,C,D,D"

MsgBox str_compare(str1, str2)

End Sub
[/Quote]

试试
倒大霉的上帝 2010-07-08
  • 打赏
  • 举报
回复

Private Sub Command1_Click()
Dim str1 As String
Dim str2 As String
Dim strArray() As String
Dim i As Integer
Dim bTheSame As Boolean

bTheSame = False
str1 = "A,C,D,D"
str2 = "C,A,D,C"

If Len(str1) <> Len(str2) Then
MsgBox "False"
Exit Sub
End If


strArray = Split(str1, ",")
str2 = "," & str2 & ","

For i = 0 To UBound(strArray)
If InStr(str2, "," & strArray(i) & ",") > 0 Then
str2 = Replace(str2, "," & strArray(i), "")
strArray(i) = ""
Else
bTheSame = False
Exit For
End If
Next i

str2 = Replace(str2, ",", "")
If Len(str2) > 0 Then
bTheSame = False
Else
If Len(Replace(Join(strArray), " ", "")) > 0 Then
bTheSame = False
Else
bTheSame = True
End If
End If

MsgBox bTheSame

End Sub
jhone99 2010-07-08
  • 打赏
  • 举报
回复

Private Sub Command1_Click()
Dim str1 As String
Dim str2 As String


str1 = "A,C,D"
str2 = "C,D"

MsgBox str_compare(str1, str2)

End Sub

Private Function str_compare(ByVal str1 As String, ByVal str2 As String) As Integer
Dim a1() As String
Dim a2() As String

a1 = Split(str1, ",")
a2 = Split(str2, ",")

If UBound(a1) <> UBound(a2) Then
str_compare = 0
Else
For i = 0 To UBound(a1)
If Not InStr(str2, a1(i)) > 0 Then
str_compare = 0
Exit For
End If
Next i

str_compare = 1
End If

End Function

7,789

社区成员

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

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