Private Sub Command1_Click()
MsgBox numonce("ss dd sd ss kj lk kj")
End Sub
Function numonce(ByVal s As String)
Dim temp() As String, i As Long
numonce = 0
s = "*" & Replace(s, " ", "* *") & "*"
temp = Split(s, " ")
For i = 0 To UBound(temp)
If UBound(Filter(temp, temp(i))) = 0 Then numonce = numonce + 1
Next
Erase temp
End Function
加强:
Sub getonce(ByVal s As String, Optional ByRef num As Integer, Optional ByRef result As String)
Dim temp() As String, i As Long, temp2() As String
num = 0
s = "*" & Replace(s, " ", "* *") & "*"
temp = Split(s, " ")
For i = 0 To UBound(temp)
If UBound(Filter(temp, temp(i))) = 0 Then
num = num + 1
ReDim Preserve temp2(1 To num)
temp2(num) = Mid(temp(i), 2, Len(temp(i)) - 2)
End If
Next
result = Join(temp2, vbCrLf)
MsgBox "只出现一次的字的个数有 " & num & " 个:" & vbCrLf & result
Erase temp
Erase temp2
End Sub
Private Sub Command2_Click()
getonce "ss dd sd ss kj lk kj"
End Sub
Private Sub Command1_Click()
Dim i, j As Integer '循环变量
Dim s As String '你的字符串
Dim aStr() As String '拆分你的字符串的数组
Dim ub As Integer '拆分得到的数组的数据个数
Dim zb As Boolean '正版标志
Dim ct As Integer '记录正版个数
Dim sout As String '输出正版集合,如果你要的话
s = "ss dd sd ss kj lk kj"
aStr = Split(s) '拆分
ub = UBound(aStr) '取得最大下标
sout = ""
ct = 0
For i = 0 To ub '排队检查
If Not aStr(i) = "" Then '如果已被刷掉就不查了
zb = True '姑且抱有一丝希望
For j = i + 1 To ub
If aStr(i) = aStr(j) Then
zb = False '完了,被盗版了
aStr(j) = "" '刷掉找到的盗版
End If
Next j
If zb = False Then
aStr(i) = "" '已被盗版,刷掉自己
Else
ct = ct + 1 '哟嗬,正版,难得,收藏先
sout = sout & " " & aStr(i)
End If
End If
Next i
Print ct & " word(s) stand alone:"
Print sout
End Sub
Option Explicit
'需要加入对Microsoft Scripting Runtime的引用
Private Sub Command1_Click()
Dim s As String
s = "ss dd sd ss kj lk kj"
Dim mDictionary As New Dictionary
Dim buff() As String
On Error Resume Next
buff = Split(s, " ")
Dim i As Long
For i = 0 To UBound(buff)
If mDictionary.Exists(buff(i)) Then
mDictionary.Item(buff(i)) = mDictionary.Item(buff(i)) + 1
Else
mDictionary.Add buff(i), 1
End If
Next
Dim mItems()
mItems = mDictionary.Items
Dim mKeys()
mKeys = mDictionary.Keys
Dim Result() As String
Dim n As Long
For i = 0 To UBound(mKeys)
If mItems(i) = 1 Then
ReDim Preserve Result(n)
Result(n) = mKeys(i)
n = n + 1
End If
Next
Debug.Print "指定字串中,只出现一次的单词有" + CStr(UBound(Result) + 1) + "个"
Debug.Print "它们是:"
For i = 0 To UBound(Result)
Debug.Print Result(i)
Next
Debug.Print "报告完毕"
'收尾工作
Erase buff
Erase mItems
Erase mKeys
Erase Result
Set mDictionary = Nothing
End Sub
Public Function Exist_Once(ByVal strText As String) As Integer
Dim i, L, n As Integer
Dim mystr, myword As String
If Right(strText, 1) > " " Then strText = strText & " "
mystr = strText
For i = 1 To Len(strText) Step 3
myword = Mid(strText, i, 3)
L = Len(mystr)
mystr = Replace(mystr, myword, "")
If L - Len(mystr) = Len(myword) Then n = n + 1
If Len(mystr) = 0 Then Exit For
Next i
Exist_Once = n
End Function