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

问题描述:

如何使用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

这将给在一个给定的子串的所有单词计数。

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

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. 移除标点符号和裂伤连续的空格,以一个单一的一个,例如与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

最简洁的答案,很好的考虑。 –

要找到匹配的字符串

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