PC Review


Reply
Thread Tools Rate Thread

Counting SubStrings and thier starting positions within Main Strin

 
 
=?Utf-8?B?RXhjZWxNb25rZXk=?=
Guest
Posts: n/a
 
      7th Feb 2006
What is the easiest way to count the number of occurences of a substring
within a string AND the starting position of each substring? For Example:

String = "dog 123 cat 452 if 6754 dog"
SubString = "dog"

Occurences of SubString = 2
Starting Position of SubString = 1 and 26

Thanks

EM
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9wcGVycw==?=
Guest
Posts: n/a
 
      7th Feb 2006
One way:

Public npos() as integer

Sub GetSubStrings(ByVal SearchString As String, FindStr As String)
n = 1
i = 0
Do
n = InStr(n, SearchString, FindStr)
If n <> 0 Then
ReDim Preserve Npos(i)
Npos(i) = n
n = n + 1
i = i + 1
End If
Loop Until n = 0
End Sub

Sub myTest()

Call GetSubStrings("dog 123 cat 452 if 6754 dog", "dog")

MsgBox "Number of strings = " & UBound(Npos) + 1
For i = 0 To UBound(Npos)
MsgBox Npos(i)
Next
End Sub

"ExcelMonkey" wrote:

> What is the easiest way to count the number of occurences of a substring
> within a string AND the starting position of each substring? For Example:
>
> String = "dog 123 cat 452 if 6754 dog"
> SubString = "dog"
>
> Occurences of SubString = 2
> Starting Position of SubString = 1 and 26
>
> Thanks
>
> EM

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      7th Feb 2006
cTimes = (Len(sString) - Len(Replace(sString, sSubstring, ""))) _
/ Len(sSubstring)
sMsg = sSubstring & " occurs" & cTimes & " times" & vbNewLine
ipos = 0
For i = 1 To cTimes
ipos = InStr(ipos + 1, sString, sSubstring)
sMsg = sMsg & "#" & i & " occurs at: " & ipos & vbNewLine
Next i

MsgBox sMsg


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"ExcelMonkey" <(E-Mail Removed)> wrote in message
news:F7E88C35-1BC0-4B24-93F7-(E-Mail Removed)...
> What is the easiest way to count the number of occurences of a substring
> within a string AND the starting position of each substring? For Example:
>
> String = "dog 123 cat 452 if 6754 dog"
> SubString = "dog"
>
> Occurences of SubString = 2
> Starting Position of SubString = 1 and 26
>
> Thanks
>
> EM



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array formulas counting substrings across rows Christi Microsoft Excel Worksheet Functions 6 24th Apr 2009 05:36 AM
Is there a string function for counting occurances of substrings? Jason Barnett Microsoft VB .NET 2 28th Oct 2005 10:41 PM
Counting unique items, disregarding thier annexs =?Utf-8?B?ZXhjZWxGYW4=?= Microsoft Excel Misc 7 16th Sep 2004 05:26 PM
Counting Cells by Thier Month Kevin Microsoft Excel Worksheet Functions 1 5th Jul 2004 01:17 PM
Re: counting cells in a column based on thier color Niek Otten Microsoft Excel Programming 0 20th Jan 2004 08:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:16 PM.