Results 1 to 40 of 54

Thread: [VB6] Faster Split & Join (development)

Threaded View

  1. #26
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,753

    Re: [VB6] Faster Split & Join (development)

    It can be corrected as follows:
    Code:
    Public Function CountChrInString(ByVal Expression As String, ByVal Character As String) As Long
    ' Returns the count of the specified character in the specified string.
    '
        Dim lngResult As Long
        Dim strParts() As String
        '
        If Len(Expression) > 0 Then
          strParts = Strings.Split(Expression, Character)
    
          lngResult = UBound(strParts, 1)
    
          If (lngResult = -1) Then
              lngResult = 0
          End If
    
          CountChrInString = lngResult
       End If
    End Function
    But I would not use a Split to count the number of characters in a string.
    Code:
    Public Function CharCount(sString As String, sChar As String) As Long
      Dim lPos As Long
      
      If Len(sString) = 0 Then Exit Function
      
      lPos = 0
      '  CharCount= 1
      Do
        lPos = InStr(lPos + 1, sString, sChar , vbBinaryCompare)
        If lPos > 0 Then CharCount= CharCount+ 1
      Loop Until lPos = 0
      
    End Function
    Last edited by Arnoutdv; Feb 24th, 2016 at 10:27 AM. Reason: The CharCount = 1 should not be there

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width