|
-
Jul 5th, 2001, 12:34 AM
#1
Counting specific chars in a string
It looks like there is no function for counting the amount of a certain character(s) in a string (or I am just dumb). Right now I've got this:
Code:
Public Function StrCount(String1, String2) As Long
For x = 1 To Len(String1)
If Mid(String1, x, Len(String2)) = String2 Then StrCount = StrCount + 1
Next x
End Function
But this seems to work too:
Code:
Public Function StrCount(String1, String2) As Long
Dim sCount() As String
sCount = Split(String1, String2)
StrCount = UBound(sCount)
End Function
But surely there is a VB function that I missed, or an API. There's a few string API functions, but none of them seem to do anything related to this...
Any Ideas?
-
Jul 5th, 2001, 12:45 AM
#2
Hyperactive Member
No, but this will be faster.
Code:
Function CountCharacter(byval strText as string, _
byval strCharsToCount, _
bval lngCompare) as long
Dim I as long
Dim lngCount as long
Dim lngInstr as long
Dim L as long
I = 1
L = Len(strCharsToCount)
lf L = 0 then exit function
Do
'lngcompare 0 = BinaryCompare, 1=textcompare
lngInstr = Instr(I,strText, _
strCharsToCount, _
lngCompare)
If lngInstr = 0 then Exit Do
lngCount = lngCount + 1
I = lngInstr + L
Loop
CountCharacter = lngCount
Last edited by John Yingling; Jul 5th, 2001 at 04:39 PM.
-
Jul 5th, 2001, 02:38 AM
#3
Registered User
VB Code:
Private Function InStrCount(Source As String, Search As String) As Long
InStrCount = Len(Source) - Len(Replace(Source, Search, ""))
End Function
-
Jul 5th, 2001, 02:55 AM
#4
Or and easy, if not necessarily fast, method:
Code:
Count = UBound(Split(TheString, TheChar)) + 1
- gaffa
-
Jul 5th, 2001, 04:05 PM
#5
The fastest seems to be the one which involves splitting and counting the array...
-
Jul 5th, 2001, 07:14 PM
#6
Registered User
Byte arrays are always the fastest appraoch to processing each character in a string.
Test the functions on a string of 500,000 characters to quantify the difference, the results might shock you.
-
Jul 5th, 2001, 07:44 PM
#7
Yeah, the strings I tested were only about 7 or 8 thou, using a 500 thou character string showed that the Byte Array was actually a fair bit faster.
-
Jul 5th, 2001, 07:46 PM
#8
Oh, Nucleus, I just realised that your code doesn't work with Carriage Return Line Feeds (vbCrLf). Doesn't matter though because that's not what I'm after.
-
Jul 5th, 2001, 08:56 PM
#9
Registered User
Yes that is because carriage return is two consecutive characters not one, are you changing the spec on me? Ha!
-
Jul 5th, 2001, 09:05 PM
#10
No no no, but I was wondering why it didn't work when I tried a multibyte constant...
-
Jul 5th, 2001, 09:13 PM
#11
He, he,
My little one liner will work for strings of any length....
I'm actually pretty surprised that it's reasonably fast, I honestly thought ity would be as slow as a wet week, but there you go.
- gaffa
-
Jul 5th, 2001, 10:53 PM
#12
Registered User
Yes the choice is easy one liner with vb where speed is not a concern using split/replace, but if speed is important then go with byte arrays.
If you want to count multibyte strings and speed is required, just yell.
-
Jul 5th, 2001, 11:00 PM
#13
Speed is an issue, but not with multibyte strings...
-
Jul 5th, 2001, 11:18 PM
#14
-
Jul 5th, 2001, 11:20 PM
#15
3300% eh? Not bad at all. In fact I'd say that's the best you could get...
-
Jul 5th, 2001, 11:40 PM
#16
Registered User
Yeah, but you have to write so much more code, which potentially introduces more bugs too. So in 99%of the cases I would go with Gaffa's one liner.
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
|