how to count a particular symbol in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nivaz
    New Member
    • Feb 2008
    • 17

    how to count a particular symbol in a file

    hai frnds i am doing my project in VB. i have to count some character sin a file.

    for ex

    nivaz is new******* here
    so plz help him******ok



    got that, from that i have to count each and every star in a line and also have to display that in a text box. how to do this help me guys. send ur replys, view to my inbox. thanks
  • VBWheaties
    New Member
    • Feb 2008
    • 145

    #2
    Originally posted by nivaz
    hai frnds i am doing my project in VB. i have to count some character sin a file.

    for ex

    nivaz is new******* here
    so plz help him******ok



    got that, from that i have to count each and every star in a line and also have to display that in a text box. how to do this help me guys. send ur replys, view to my inbox. thanks
    You want the split function. This will return an array. From an array, you can count how many values there are using UBOUND.

    Example:

    Code:
    Dim MyString As String 
    Dim Astericks
    Dim AsterickCount 
    
    MyString = "This is my mother****ing string!!!"
    
    Astericks = Split(MyString, "*") 
    
    AsterickCount = UBOUND(Astericks)

    On the other hand, you probably havent learned that function (Split or Ubound).
    In that case, you will need to iterate each character in a loop.
    I'm certain you were taught Instr and/or Mid.

    so..

    Code:
    Dim i As Integer
    Dim MyString As String 
    Dim iCount as Integer
    Dim sChar As String 
    
    For i = 1 to Len(MyString) 
       sChar = MID(MyString, i, 1) 
       If sChar = "*" Then 
          iCount = iCount + 1
       End If 
    Next i 
    
    msgbox "There are " & iCount & " astericks in my string!"

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      Originally posted by VBWheaties
      ...
      I'm certain you were taught Instr and/or Mid.
      ...
      I think Instr could be faster than Mid:

      [CODE=vb]
      Dim i As Long
      Dim MyString As String
      Dim iCount as Integer
      Dim sChar As String
      do
      i=instr(i+1,mys tring,schar)
      if i = 0 then exit do
      icount = icount+1
      loop until i > len(mystring) 'this is just in case the last character of the string is sChar.
      msgbox "There are " & iCount & " " & sChar & " in my string!"
      [/CODE]

      Anyway, using split and ubound will make things easier and faster than any of this two.

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by kadghar
        Anyway, using split and ubound will make things easier and faster than any of this two.
        My mistake, it'll only make things easier. Instr is the fastest, then Mid, and split-ubound is the slowest way (see, i've got so much free time =P):

        [CODE=vb]Sub TimeTest()
        Dim a() As String
        Dim str1 As String
        Dim i As Long
        Dim t(2) As Single
        Dim r(2) As Long

        str1 = string(250000," *")

        t(0) = Timer
        a = Split(str1, "*")
        r(0) = UBound(a)
        t(0) = Timer - t(0)

        t(1) = Timer
        For i = 1 To Len(str1)
        If Mid(str1, i, 1) = "*" Then
        r(1) = r(1) + 1
        End If
        Next i
        t(1) = Timer - t(1)

        t(2) = Timer
        i = 0
        Do
        i = InStr(i + 1, str1, "*")
        If i = 0 Then Exit Do
        r(2) = r(2) + 1
        Loop Until i > Len(str1)
        t(2) = Timer - t(2)
        MsgBox t(0) & " - " & r(0) & Chr(13) _
        & t(1) & " - " & r(1) & Chr(13) _
        & t(2) & " - " & r(2) & Chr(13)
        End Sub[/CODE]

        Note : Of course the Split method could be faster if there are only a few * in the string, this will make the array smaller and faster as well. while the MID will be as fast-slow as it is. Anyway, i'd say the Instr is the more convenient.
        Last edited by kadghar; Feb 19 '08, 07:49 PM. Reason: Add note

        Comment

        Working...