如何使用VBScript

时间:2016-10-17 06:58:15

标签: string vbscript qtp hp-uft

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

例如,如果字符串是

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

我需要在上面的字符串中重复的子字符串。它的数量。

由于

5 个答案:

答案 0 :(得分: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 :(得分:1)

通过4个简单步骤查找单词重复:

  1. 从字符串中移除interpunction并将连续空格连接到单个空格,例如使用regular expression replacement

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

  3. 将每个单词作为键放入Dictionary。如果单词已经exists

  4. ,则增加密钥的值
  5. Iterate超过字典的keys并输出具有最高值的键和值。

    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)
    

答案 2 :(得分:1)

mask = df.t1 <= df.t2
print (mask)
0    False
dtype: bool

答案 3 :(得分: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()获取一系列单词。将每一个添加到字典中,如果它错误,它就是一个字典。

答案 4 :(得分: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
'*********************************************************************************