hi Dirk,
Dirk said:
I suspect, though I haven't tested it, that this method is faster than
the Split method I proposed.
Just a simple test with GetTickCount():
Const MAX_COUNT As Long = 100000
Const STR_TEST As String = "this is a test."
Dim lngChar As Long
Dim lngChars As Long
Dim lngCount As Long
Dim lngNumSpaces As Long
Dim lngTickEnd As Long
Dim lngTickStart As Long
lngTickStart = GetTickCount()
For lngCount = 1 To MAX_COUNT
lngNumSpaces = UBound(Split(STR_TEST))
Next lngCount
lngTickEnd = GetTickCount()
Debug.Print "First: "; lngTickEnd - lngTickStart; _
" Num Spaces: "; lngNumSpaces
lngTickStart = GetTickCount()
For lngCount = 1 To MAX_COUNT
lngNumSpaces = Len(STR_TEST) - Len(Replace(STR_TEST, " ", ""))
Next lngCount
lngTickEnd = GetTickCount()
Debug.Print "Second: "; lngTickEnd - lngTickStart; _
" Num Spaces: "; lngNumSpaces
lngTickStart = GetTickCount()
lngChars = Len(STR_TEST)
For lngCount = 1 To MAX_COUNT
lngNumSpaces = 0
For lngChar = 1 To lngChars
If Mid$(STR_TEST, lngChar, 1) = " " Then
lngNumSpaces = lngNumSpaces + 1
End If
Next lngChar
Next lngCount
lngTickEnd = GetTickCount()
Debug.Print "Third: "; lngTickEnd - lngTickStart; _
" Num Spaces: "; lngNumSpaces
The Split() methode seems to be the fastest. Also using a very long
string will yield the same ranking.
mfG
--> stefan <--