-
January 9th, 2008, 12:32 PM
#1
Count # repeats of string within string
Anyone know of a nifty VB command that will tell you the number or times a string appears within another string?
For example, let's say we have a string that will be placed in a txtBox where .multiline=True. It'd be nice to know how many CrLfs are in the string so you can determine the # of lines to be displayed.
It's easy enough to write code to do this, but maybe VB does it for me. Thanks...
-
January 9th, 2008, 01:15 PM
#2
Re: Count # repeats of string within string
Yes there is this very clever line out of Davids Catskin-program:
n = Len(Text1.Text) -Len(Replace(Text1.Text, vbLF, ""))
n is the number of vbLf chars in the Text1.Text, but you will watch out: if the last line has no CrLf at the end it is not the actual number of lines.
You could also get the number of lines by using the SendMessage() API
Code:
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long _
) As Long
Private Const EM_GETLINECOUNT = &HBA
n = SendMessage(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
-
January 9th, 2008, 02:34 PM
#3
Re: Count # repeats of string within string
Excellent! How obvious! Thanks very much!
-
January 9th, 2008, 06:20 PM
#4
Re: Count # repeats of string within string
This also works:
Code:
Private Function NumberInString(sSource As String, sMatch As String) As Long
Dim sTemp() As String
sTemp = Split(sSource, sMatch)
NumberInString = UBound(sTemp)
End Function
Calling code example:
Code:
Debug.Print NumberInString("abcabcabc", "bc")
Resist the temptation to single line the call, ie:
Code:
Debug.Print UBound(Split(sSource, sMatch))
Passing the return array of a function directly to UBound is a known bug in VB6 and will leak memory. Note that this method of WoF's...
Code:
n = Len(Text1.Text) -Len(Replace(Text1.Text, vbLF, ""))
...only works for single characters.
@WoF -- Nice call on the API. Didn't know you could do that.
Last edited by Comintern; January 9th, 2008 at 06:23 PM.
Reason: Add note.
-
January 10th, 2008, 08:27 AM
#5
Re: Count # repeats of string within string
You can expand the method I used also to multiple characters. You only have to divide the result by the len of the searchstring.
Code:
n = (Len(Text1.Text) -Len(Replace(Text1.Text, StringToCount, ""))) / Len(StringToCount)
I'd like to point out again that both, this method and your NumberInString() work perfectly for the purpose of counting characters, but will also trip when used for counting lines by the presence of CrLf. If the last line is not terminated with a CrLf, as it is often, it will not count. So the linecount could be incorrect. SendMessage() gives the real linecount.
Yes, I found the SendMessage() a sheer bottmless resource for functions I always wanted, like the LB_SELECTSTRING and much more.
I wonder if there is ever a complete documentation which wraps everything up. It's always so much trouble to find things out. You try and think up complex solutions and then you find out, there is a SendMessage constant which does the trick.
Last edited by WoF; January 10th, 2008 at 08:32 AM.
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
|