Results 1 to 4 of 4

Thread: [VB] - Occurrences of a char in a string

Threaded View

  1. #1

    Thread Starter
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747

    [VB] - Occurrences of a char in a string

    V suprised I couldn't find a general purpose function that returns the number of times a character appears in a string...

    so here's one i made

    VB Code:
    1. Public Function OccurrenceInstr(searchStr As String, char As String) As Integer
    2.     Dim lastSpot As Integer
    3.     Dim occCount As Integer
    4.     Dim found As Boolean
    5.    
    6.     If searchStr = "" Or char = "" Then Exit Function
    7.    
    8.     found = True
    9.     While found
    10.         lastSpot = InStr(lastSpot + 1, searchStr, char)
    11.         If lastSpot > 0 Then
    12.             occCount = occCount + 1
    13.             found = True
    14.         Else
    15.             found = False
    16.         End If
    17.     Wend
    18.     OccurrenceInstr = occCount
    19. End Function

    note: not thoroughly tested
    Last edited by SkiNLaB; Feb 5th, 2004 at 11:26 PM.

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