如何使用 VBScript 在主字符串中查找重复的子字符串
How to find repeated sub string in a main string using VBScript
如何使用 VBScript 在主字符串中查找重复的子字符串?
例如,如果字符串是
str = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
我需要在上面的字符串中重复的子字符串。也是它的计数。
谢谢
Sub DeDup
Set Dict = CreateObject("Scripting.Dictionary")
Do Until Inp.AtEndOfStream
On Error Resume Next
Line=Inp.readline
Dict.Add Line, ""
If Err.Number <> 0 then
If LCase(Arg(1)) = "l" then
Dict.Remove Line
Dict.Add Line, ""
End If
End If
Loop
For Each thing in Dict.Keys()
Outp.writeline thing
Next
End Sub
这使用脚本字典来删除重复行。您可以使用 Split()
获取单词数组。将每一个都添加到字典中,如果错误则为重复。
通过 4 个简单的步骤查找重复的单词:
从字符串中删除标点符号并将连续的空格打乱为单个空格,例如regular expression replacement.
Set re = New RegExp
re.Pattern = " *[.,;!?'""_-] +| +"
re.Global = True
str = re.Replace(str, " ")
Split空格处的字符串。
将每个单词作为key放入Dictionary
. Increment the value for the key if the word already exists.
Iterate over the keys的字典,输出最大的key和value。
For Each word In dict.Keys
If IsEmpty(mfu) Then
mfu = word
ElseIf dict(word) > dict(mfu) Then
mfu = word
End If
Next
WScript.Echo mfu & ": " & dict(mfu)
这将给出给定子字符串中所有单词的计数。
str = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
Function RemoveDuplicates(str)
If Trim(str) = "" Then
RemoveDuplicates = Array()
Exit Function
End If
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare 'make dictionary case-insensitive
For Each elem In Split(str)
d(elem) = True
Next
RemoveDuplicates = d.Keys
End Function
sUniques = RemoveDuplicates(str)
For k = 0 To UBound(sUniques)
iCount = len(str) - len(replace(str, sUniques(k), ""))
msgbox "The string " & sUniques(k) & " appeared " & iCount/len(sUniques(k)) & " times"
Next
使用
中的第一个函数
查找出现次数:
baseString = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
subString = "Google"
MsgBox "The "& chr(34) & subString & chr(34) & " appeared " &_
findOccurancesCount(baseString, subString) & " times !" & vbCrLF &_
"in " & vbCrLF & chr(34) & baseString & chr(34)_
,vbInformation,"FindOccurancesCount"
'*********************************************************************************
Function findOccurancesCount(baseString, subString)
occurancesCount = 0
i = 1
Do
foundPosition = InStr(i, Lcase(baseString), Lcase(subString))
If foundPosition > 0 Then
occurancesCount = occurancesCount + 1
i = foundPosition + 1
End If
Loop While foundPosition <> 0
findOccurancesCount = occurancesCount
End Function
'*********************************************************************************
str = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
str1 = Split(replace(str,",","")," ")
Set dic1 = CreateObject("Scripting.Dictionary")
On Error Resume next
For Each a in str1
dic1.Add a,"1"
If Err.Number <> 0 Then
dic1(a) = cstr(cint(dic1(a)) + 1)
err.clear
End If
Next
On Error Goto 0
repeatedwords = ""
For each keys in dic1
If cint(dic1(keys)) > 1 Then
repeatedwords = repeatedwords & vbNewline & vbNewline & keys & " repeated " & dic1(keys) & " times"
End If
Next
msgbox repeatedwords
Set dic1 = nothing
如何使用 VBScript 在主字符串中查找重复的子字符串?
例如,如果字符串是
str = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
我需要在上面的字符串中重复的子字符串。也是它的计数。
谢谢
Sub DeDup
Set Dict = CreateObject("Scripting.Dictionary")
Do Until Inp.AtEndOfStream
On Error Resume Next
Line=Inp.readline
Dict.Add Line, ""
If Err.Number <> 0 then
If LCase(Arg(1)) = "l" then
Dict.Remove Line
Dict.Add Line, ""
End If
End If
Loop
For Each thing in Dict.Keys()
Outp.writeline thing
Next
End Sub
这使用脚本字典来删除重复行。您可以使用 Split()
获取单词数组。将每一个都添加到字典中,如果错误则为重复。
通过 4 个简单的步骤查找重复的单词:
从字符串中删除标点符号并将连续的空格打乱为单个空格,例如regular expression replacement.
Set re = New RegExp re.Pattern = " *[.,;!?'""_-] +| +" re.Global = True str = re.Replace(str, " ")
Split空格处的字符串。
将每个单词作为key放入
Dictionary
. Increment the value for the key if the word already exists.Iterate over the keys的字典,输出最大的key和value。
For Each word In dict.Keys If IsEmpty(mfu) Then mfu = word ElseIf dict(word) > dict(mfu) Then mfu = word End If Next WScript.Echo mfu & ": " & dict(mfu)
这将给出给定子字符串中所有单词的计数。
str = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
Function RemoveDuplicates(str)
If Trim(str) = "" Then
RemoveDuplicates = Array()
Exit Function
End If
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare 'make dictionary case-insensitive
For Each elem In Split(str)
d(elem) = True
Next
RemoveDuplicates = d.Keys
End Function
sUniques = RemoveDuplicates(str)
For k = 0 To UBound(sUniques)
iCount = len(str) - len(replace(str, sUniques(k), ""))
msgbox "The string " & sUniques(k) & " appeared " & iCount/len(sUniques(k)) & " times"
Next
使用
中的第一个函数查找出现次数:
baseString = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
subString = "Google"
MsgBox "The "& chr(34) & subString & chr(34) & " appeared " &_
findOccurancesCount(baseString, subString) & " times !" & vbCrLF &_
"in " & vbCrLF & chr(34) & baseString & chr(34)_
,vbInformation,"FindOccurancesCount"
'*********************************************************************************
Function findOccurancesCount(baseString, subString)
occurancesCount = 0
i = 1
Do
foundPosition = InStr(i, Lcase(baseString), Lcase(subString))
If foundPosition > 0 Then
occurancesCount = occurancesCount + 1
i = foundPosition + 1
End If
Loop While foundPosition <> 0
findOccurancesCount = occurancesCount
End Function
'*********************************************************************************
str = "Google mail, Google Maps, Google drive, Google music, Google play, Google office"
str1 = Split(replace(str,",","")," ")
Set dic1 = CreateObject("Scripting.Dictionary")
On Error Resume next
For Each a in str1
dic1.Add a,"1"
If Err.Number <> 0 Then
dic1(a) = cstr(cint(dic1(a)) + 1)
err.clear
End If
Next
On Error Goto 0
repeatedwords = ""
For each keys in dic1
If cint(dic1(keys)) > 1 Then
repeatedwords = repeatedwords & vbNewline & vbNewline & keys & " repeated " & dic1(keys) & " times"
End If
Next
msgbox repeatedwords
Set dic1 = nothing