|
-
Aug 11th, 2015, 02:30 AM
#1
Thread Starter
Fanatic Member
Count Sequence string inside other string
Hi
How can I to count a string sequence inside other string
Example
1X21X2XX122X12
I need to do statistics on how often appears a sequence of three characters eg
1X2 ==>2
12X ==> 0
X12 ==> 2
X21 ==> 1
21X ==> 1
2X1 ==> 1
How can I to do it reading a text file and counting and summarizing ?
-
Aug 11th, 2015, 06:50 AM
#2
Re: Count Sequence string inside other string
I'm sure you'll get a few different options but here's one way.
Code:
Private Sub Form_Load()
Text1.Text = "1X21X2XX122X12"
End Sub
Private Sub Command1_Click()
Dim dictSeries As Dictionary ' need to set a reference to the Microsoft Scripting Runtime
Dim i As Integer
Dim strSeries As String
Dim series
List1.Clear
'Store words and word count in a dictionary object
Set dictSeries = New Dictionary
For i = 1 To Len(Text1.Text) - 2
strSeries = Mid(Text1.Text, i, 3)
If dictSeries.Exists(strSeries) Then
dictSeries(strSeries) = Int(dictSeries(strSeries)) + 1
Else
dictSeries.Add strSeries, 1
End If
Next i
For Each series In dictSeries.Keys
List1.AddItem series & " ==> " & dictSeries(series)
Next series
Set dictSeries = Nothing
End Sub
-
Aug 11th, 2015, 01:02 PM
#3
Re: Count Sequence string inside other string
Quick question...will you know in advance what 3 characters are unique in the string, OR, will the 3 unique characters always be the first three characters in the string?
(I'm sure what MarkT has posted is a solution, I just wanted to crank up one for you as well, but an answer to that question would help, otherwise I'd have to loop through and find the three unique characters.
-
Aug 11th, 2015, 01:57 PM
#4
Re: Count Sequence string inside other string
Well, if not known, you can find the three unique characters, put them into an array, and then call a display function like below:
Code:
Option Explicit
Private Sub Command1_Click()
'IF NOT KNOWN, determine what three distinct characters make up the entire string
Dim myThreeCharString As String
Dim myArray(2) As String
Dim x As Integer
Dim y As Integer
myThreeCharString = Text1.Text
myArray(0) = Mid(myThreeCharString, 1, 1)
For x = 2 To Len(myThreeCharString)
If Mid(myThreeCharString, x, 1) <> myArray(0) Then
myArray(1) = Mid(myThreeCharString, x, 1)
Exit For
End If
Next x
For y = x To Len(myThreeCharString) - x
If Mid(myThreeCharString, y, 1) <> myArray(0) And Mid(myThreeCharString, y, 1) <> myArray(1) Then
myArray(2) = Mid(myThreeCharString, y, 1)
Exit For
End If
Next y
'ONCE distinct three characters are known and put into myArray(), call StringToSearch function
InstrCount Text1.Text, myArray(0) & myArray(1) & myArray(2)
InstrCount Text1.Text, myArray(0) & myArray(2) & myArray(1)
InstrCount Text1.Text, myArray(1) & myArray(0) & myArray(2)
InstrCount Text1.Text, myArray(1) & myArray(2) & myArray(0)
InstrCount Text1.Text, myArray(2) & myArray(1) & myArray(0)
InstrCount Text1.Text, myArray(2) & myArray(0) & myArray(1)
End Sub
Function InstrCount(StringToSearch As String, StringToFind As String) As Long
If Len(StringToFind) Then
InstrCount = UBound(Split(StringToSearch, StringToFind))
End If
Debug.Print StringToFind & " -- " & CStr(InstrCount)
End Function
Private Sub Form_Load()
Text1.Text = "1X21X2XX122X12"
End Sub
-
Aug 11th, 2015, 02:36 PM
#5
Re: Count Sequence string inside other string
You guys get totally different results. Sam gets exactly same results as the results OP posted. MT thinks OP wants to count any and all three character sets and I personally thought that MT's approach was what OP wanted by the way OP explained it (not by what OP posted as results) but Sam thinks OP wants a certain set of three characters.
What I am not sure of did OP post results as an example only or as exactly what he wanted from that particular string. Makes a BIG difference.
Now, I am not too sure anymore
Last edited by jmsrickland; Aug 11th, 2015 at 03:01 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 11th, 2015, 04:37 PM
#6
Re: Count Sequence string inside other string
Yeah...sometimes difficult. My rationale was his example....did not know if he already knew the three characters or not, so had to find them. I also forgot to add the SUM of the number of times the three-character strings are found....this does that:
Code:
Option ExplicitDim totalCount As Integer
Private Sub Command1_Click()
'IF NOT KNOWN, determine what three distinct characters make up the entire string
Dim myThreeCharString As String
Dim myArray(2) As String
Dim x As Integer
Dim y As Integer
myThreeCharString = Text1.Text
myArray(0) = Mid(myThreeCharString, 1, 1)
For x = 2 To Len(myThreeCharString)
If Mid(myThreeCharString, x, 1) <> myArray(0) Then
myArray(1) = Mid(myThreeCharString, x, 1)
Exit For
End If
Next x
For y = x To Len(myThreeCharString) - x
If Mid(myThreeCharString, y, 1) <> myArray(0) And Mid(myThreeCharString, y, 1) <> myArray(1) Then
myArray(2) = Mid(myThreeCharString, y, 1)
Exit For
End If
Next y
'ONCE distinct three characters are known and put into myArray(), call StringToSearch function
InstrCount Text1.Text, myArray(0) & myArray(1) & myArray(2)
InstrCount Text1.Text, myArray(0) & myArray(2) & myArray(1)
InstrCount Text1.Text, myArray(1) & myArray(0) & myArray(2)
InstrCount Text1.Text, myArray(1) & myArray(2) & myArray(0)
InstrCount Text1.Text, myArray(2) & myArray(1) & myArray(0)
InstrCount Text1.Text, myArray(2) & myArray(0) & myArray(1)
Debug.Print "Total found = " & CStr(totalCount)
End Sub
Function InstrCount(StringToSearch As String, StringToFind As String) As Long
If Len(StringToFind) Then
InstrCount = UBound(Split(StringToSearch, StringToFind))
totalCount = totalCount + InstrCount
End If
Debug.Print StringToFind & " -- " & CStr(InstrCount)
End Function
Private Sub Form_Load()
Text1.Text = "1X21X2XX122X12"
End Sub
-
Aug 11th, 2015, 05:08 PM
#7
Re: Count Sequence string inside other string
Rethinking and rereading the 1st post I now think that OP wants to see how many times a certain set of three characters exist in the string. These certain sets equate down to the sets OP posted in his results. So, I have to think that Sam's approach is the correct one.
Even so, I really like what MT posted.
How often do these threads in addition to trying to resolve the OP's requests also turn out to be somewhat of a guessing game challenge as to what the is OP saying
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 11th, 2015, 05:22 PM
#8
Re: Count Sequence string inside other string
All too often.
I suspect that in many cases the reason someone comes asking for help is they actually have trouble clearly defining the problem they want to solve. Usually that's more than half of the battle and once accomplished there isn't much left to do but express it in correct code.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|