Results 1 to 7 of 7

Thread: string functions ...

  1. #1
    Guest
    hi !

    is there any function which returns the no of ocurances of a sting in another.

    eg, 123456,67,78,75,86,99
    i want to find how many commas are there in this string

    thanx

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Public Function GetCount(Character As String, FindMe As String)
        
        Dim myLen As Integer
        Dim iCount As Integer
        Dim FoundMe As Integer
        myLen = Len(Character)
    
        For iCount = 1 To myLen
          If Left(Character, 1) = FindMe Then
               FoundMe = FoundMe + 1
              End If
          
          Character = Right(Character, (myLen - iCount))
          
       Next
     GetCount = FoundMe
    End Function
    
    Private Sub Command1_Click()
    
        Dim myString As String
        myString = "123456,67,78,75,86,99"
        MsgBox GetCount(myString, ",")
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Or use InStr

    Code:
    Function GetOccurrence(strToSearch As String, strToFind As String, iFound As Integer) As Integer
    
    Dim iPos as Integer, strName as String
    
    Do While InStr(iPos + 1, strToSearch, strToFind) <> 0
      iFound = iFound + 1
      iPos = InStr(iPos + 1, strToSearch, strToFind)
    Loop
    
    Return iFound
    
    End Function
    You would call the function: GetOccurrence("123456,67,78,75,86,99", ",", 0)

    Cheers,

    P.

    Not nearly so tired now...

    Haven't been around much so be gentle...

  4. #4
    Guest
    Or, for VB6 (and higher?) a one-liner!

    Code:
    Private Function GetOccurances(strSearchMe As String, strToFind As String)
        GetOccurances = UBound(Split(strSearchMe, strToFind, , vbTextCompare))
    End Function
    Usage:
    Code:
        Count= GetOccurances("123456,67,78,75,86,99", ",")
    Enjoy!

  5. #5
    Guest
    By the way... Did you notice the function doesn't even use a variabele?


  6. #6
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    You're right - its an encapsulation of InStr - far too easy to use and no variables - pah! Must be for wimps!

    Good spot.

    P.

    P.S. Can you see my Access 97 roots showing?
    Not nearly so tired now...

    Haven't been around much so be gentle...

  7. #7
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    You're right - its an encapsulation of InStr - far too easy to use and no variables - pah! Must be for wimps!

    Good spot.

    P.

    P.S. Can you see my Access 97 roots showing?
    Not nearly so tired now...

    Haven't been around much so be gentle...

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