2016-10-17 60 views
0

如何使用VBScript在主字符串中查找重複的子字符串?如何使用VBScript在主字符串中查找重複的子字符串

例如,如果該字符串是

str = "Google mail, Google Maps, Google drive, Google music, Google play, Google office" 

我所需要的,其在上面的字符串重複的子字符串。也是它的數量。

由於

+1

我不知道爲什麼人們都反對投票這一點。這不是一個有效的問題嗎?或者我應該假設投票的人不知道答案。 如果你添加一個原因作爲投票的評論,效果會更好。 – RamaKrishna

+0

用'Instr'研究'do ... loop'' – 2016-10-17 07:35:15

+0

'Instr(str,「Google」)' 這就是你說的,但我不想明確地傳遞'google'。它應該自動地找到重複的單詞。 – RamaKrishna

回答

2

這將給在一個給定的子串的所有單詞計數。

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 

首先使用功能從https://stackoverflow.com/a/20310733/2571523

+1

非常感謝Ankur jain。這對我完美的工作。 不希望你的答案。 Iam是UFT博客的追隨者... – RamaKrishna

0
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()來獲取一組單詞。將每一個添加到字典中,如果錯誤是dup。在4個簡單的步驟

1

查找字重複:從字符串

  1. 移除標點符號和裂傷連續的空格,以一個單一的一個,例如與regular expression replacement

    Set re = New RegExp 
    re.Pattern = " *[.,;!?'""_-] +| +" 
    re.Global = True 
    str = re.Replace(str, " ") 
    
  2. Split字符串在空格處。

  3. 將每個單詞作爲關鍵字放入Dictionary。如果字已經爲exists,則增加該值的值。

  4. Iterate over the keys of the dictionary and output the key and value with the highest 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) 
    
+0

最簡潔的答案,很好的考慮。 –

0

要找到匹配的字符串

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 
'********************************************************************************* 
1
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 
相關問題