0

How to find repeated sub string in a main string using VBScript?

For example, if the string is

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

I need the the substring which is repeated in above string. Also its count.

Thanks

4
  • 1
    I don't know why people are down voting this. Is this not a valid question or should I assume that people who down voted don't know the answer. Better if u add a reason as comment for down voting. Commented Oct 17, 2016 at 7:27
  • Look into a do ... loop with Instr Commented Oct 17, 2016 at 7:35
  • Instr(str,"Google") this is what you say, but i don't want to pass 'google' explicitly..It should automatically find the repeated word. Commented Oct 17, 2016 at 8:26
  • 1
    I presume people are downvoting, because your question is basically asking "please write code for me", which is not appreciated here. Commented Oct 17, 2016 at 9:44

5 Answers 5

2

This will give the count of all words in a given substring.

 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

Using First function from https://stackoverflow.com/a/20310733/2571523

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ankur jain. This worked for me perfectly. Idid not expect answer from you. Iam the follower of your UFT blog...
1

Finding word repetitions in 4 simple steps:

  1. Remove interpunction from the string and mangle consecutive spaces to a single one, e.g. with a regular expression replacement.

    Set re = New RegExp
    re.Pattern = " *[.,;!?'""_-] +| +"
    re.Global  = True
    str = re.Replace(str, " ")
    
  2. Split the string at spaces.

  3. Put each word as a key into a Dictionary. Increment the value for the key if the word already 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)
    

1 Comment

Most succinct answer, nicely considered.
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

Comments

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

This uses a scripting dictionary to dedup lines. You can get an array of words by using the Split(). Add each one to a dictionary, if it errors it's a dup.

Comments

0

To find occurances :

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
'*********************************************************************************

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.