Results 1 to 4 of 4

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

  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.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox CountChars("Hello there Lenny", "a")
    3. End Sub
    4.  
    5. Private Function CountChars(ByVal SearchString As String, _
    6.                             ByVal StrToFind As String, _
    7.                             Optional ByVal IgnoreCase As Boolean = True) _
    8.                             As Integer
    9.                            
    10.     If Len(SearchString) = 0 Or Len(StrToFind) = 0 Then Exit Function
    11.    
    12.     If IgnoreCase Then
    13.         SearchString = LCase$(SearchString)
    14.         StrToFind = LCase$(StrToFind)
    15.     End If
    16.    
    17.     CountChars = UBound(Split(SearchString, StrToFind))
    18.  
    19. End Function

    Probably better not to do a lot of looping.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

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

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Another way

    VB Code:
    1. Option Explicit
    2.  
    3. Private Function CharCount(S As String, Char As String) As Integer
    4.     If Len(Char) = 0 Then Exit Function
    5.     Dim SR As String
    6.    
    7.     SR = Replace(S, Char, vbNullString)
    8.    
    9.     CharCount = (Len(S) - Len(SR)) / Len(Char)
    10. End Function
    11.  
    12.  
    13. Private Sub Form_Load()
    14.     MsgBox CharCount("manavo", "a")
    15. End Sub
    Last edited by manavo11; Feb 7th, 2004 at 05:48 PM.


    Has someone helped you? Then you can Rate their helpful post.

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