Results 1 to 3 of 3

Thread: Need a VB function to tell me...

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 1999
    Location
    Ohio
    Posts
    59

    Post

    How many times a character occurs in a string.
    For example: I have a string "1.2.3.4" and I want a function to return the value 3 because "." occurs 3 times in the string.

    Does anyone have such a function?


  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Code:
    Const TEXT = "1.2.3.4"
        Const CHAR = "."
        Dim intCount As Integer
        Dim intIndex As Integer
        
        For intIndex = 1 To Len(TEXT)
            If Mid$(TEXT, intIndex, 1) = CHAR Then
                intCount = intCount + 1
            End If
        Next

    ------------------
    Marty
    What did the fish say when it hit the concrete wall?
    > > > > > "Dam!"

  3. #3
    Junior Member
    Join Date
    Sep 1999
    Posts
    19

    Post

    If you have VB6:

    dim text as string
    dim ans as integer

    text = "1.2.3.4"
    ans = Len(text) - Len(Replace(text, ".", ""))

    HTH


    ------------------
    LM Ginn


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